0

I'm creating a web page which parses a text file and outputs "important" line numbers as hyperlinks that can be clicked by the user and then taken to that line number of the inputted text file. Naturally this means that by clicking the hyperlink the browser must navigate to the text file. The problem lies with that part. When the hyperlinks are clicked, nothing happens. No errors, just nothing.

Here's the relevant cshtml code:

<pre>
    @results

    @for (int i = 0; i < ViewBag.links.Count; i++)
    { 
        if (ViewBag.links[i].isHyperLink == true) 
        {
            {<a href="@ViewBag.links[i].url">@ViewBag.links[i].text</a>} //HYPERLINKS PRINTED TO PAGE HERE
        }  
        if (ViewBag.links[i].isHyperLink == false)
        {
            @ViewBag.links[i].text;
        }
    }
</pre>

Here's the relevant C# code:

        MatchCollection mc = Regex.Matches(output, @", [0-9]+?\)"); //find matches in txt file
        List<TextandHyperlink> links = new List<TextandHyperlink>(); //create list of hyperlink objects
        TextandHyperlink tempLink = new TextandHyperlink(); //create 

        int length;
        string temp = "";

        // Get line numbers
        for (int i = 0; i < mc.Count; i++)
        {
            if (i > 0)
                tempLink = new TextandHyperlink();

            length = mc[i].Value.Length;
            temp = mc[i].Value.Substring(2, length - 3);
            tempLink.text = temp;
            tempLink.url = relativePath; //ASSIGN PATH TO TXT FILE HERE
            tempLink.isHyperLink = true;
            links.Add(tempLink);
        }
        ViewBag.links = links;

I've verified that relativePath is the correct path to the text file on my pc. Just in case, here's an example of a path I'm using: relativePath = C:\Users\USER\PROJECT\Website\Website\Home\UploadFiles~/App_Data/Uploads/bloop.txt

Thanks in advance for any help!

edit: Here is the generated html:

--------
Summary
--------
Version             23.00.00.00
Error               2   at lines    (09/02/2014-06:13:32.607, <a href="C:\Users\USER\PROJECT\Website\Website\Home\UploadFiles~/App_Data/Uploads/bloop.txt">5662</a>); (09/02/2014-06:15:10.756, <a href="C:\Users\USER\PROJECT\Website\Website\Home\UploadFiles~/App_Data/Uploads/bloop.txt">7397</a>)
Timeouts                0
Inits               1   at lines    (09/02/2014-06:12:59.900, <a href="C:\Users\USER\PROJECT\Website\Website\Home\UploadFiles~/App_Data/Uploads/bloop.txt">2</a>)
7
  • Post the HTML that gets generated. The error is probably there...
    – David
    Dec 8, 2014 at 20:33
  • Have you tried Path.Combine? For setting the path. Dec 8, 2014 at 20:37
  • I've posted the generated html as an edit. Yes I am using Path.Combine to create relativePath.
    – Grant Ball
    Dec 8, 2014 at 20:42
  • You can't link to filesystem-style path without using the file:// protocol, i.e. the URL would have to be file://C/Users.... However, even then, that will only work running locally because of browser integration with the local file system. Remotely, you'd need to serve the file through your webserver using an actual URL relative to your site's document root. Dec 8, 2014 at 21:23
  • You can't link to filesystem-style path without using the file:// protocol, i.e. the URL would have to be file://C/Users.... However, even then, that will only work running locally because of browser integration with the local file system. Remotely, you'd need to serve the file through your webserver using an actual URL relative to your site's document root. Dec 8, 2014 at 21:23

1 Answer 1

0

If you know for sure that your path and filename are correct, then Permissions will be another issue. On a Windows Server machine for example, you will have to ensure that the same 'user' running the exe / web page / operation (could be a local account, network service account, etc) has at least explicit read rights on that file. Accessing files from web pages is deliberately difficult as there has to be security mechanisms in place, permissions is one of them.

(Mind you - in your error messages - your file path does look dodgey...The floating tilda is suspicious)

9
  • After reading your comment, I moved the text file to a folder that certainly has no permission limitations (and updated relativePath, of course) but I still have the same problem. The new file path also had no tilda in it so I'm guessing it's something else. I've read that loading files on localhost is "harder" than when the site is actually hosted. I could not find a good answer as to why or how to fix it though.
    – Grant Ball
    Dec 8, 2014 at 20:56
  • Do you want the user to open the file when they click on the link? I don't think href is enough information. Have you tried file:/// as the prefix? Try copying your link into a browser and see what happens
    – Grantly
    Dec 8, 2014 at 21:01
  • Sorry just an adjunct, file links can be tricky - and sensitive to each application. Best bet is form the link manually first and verify it in a browser, then simple web page, then in code.
    – Grantly
    Dec 8, 2014 at 21:13
  • Okay I've changed the filepath to not include the tilda. When I copy that into a browser directly it works. But when I input that same path to an href it still does nothing. I tried putting file:/// prefix as well to no avail. What do you mean that href is not enough information?
    – Grant Ball
    Dec 8, 2014 at 21:14
  • @GrantBall try this link, they have good examples of how to structure href to include file://// ... stackoverflow.com/questions/1474702/…
    – Grantly
    Dec 8, 2014 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.