1

What im doing is I have a dynamic table and creating rows which all works fine. In one of the cells im trying to create a link which when clicked will open a file. But when I clicked the link it give me an error saying cannot find server at file. Then what I do to check the file is I type the location into the address bar and it finds it but changes the address to (file:///c:/inetpub/wwwroot/test1.txt). So what I did was put that address into the anchor tag but then its still doesnt work. Here is what I have so far. Any help would be very appreciated.

tblrow = New TableHeaderRow
tblcell = New TableHeaderCell
tblcell.Text = "<a href='C://inetpub/wwwroot/test1.txt' target='_blank'>" & Test &   "</a>"
 tblrow.Cells.Add(tblcell)
 tableName.Rows.Add(tblrow)
Will
  • 1,084
  • 5
  • 20
  • 42

3 Answers3

5

Use a url and not a physical path.

Change

<a href='C://inetpub/wwwroot/test1.txt' target='_blank'>

by an absolute url location

<a href='http://DOMAIN/test1.txt' target='_blank'>

or a relative url location if the file is located on your site

<a href='RELATIVE_PATH/test1.txt' target='_blank'>
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0
  1. Most of the OS don't support with direct file call because of security reasons. Either you have to use temp location or load the file from server.

  2. Your file should be in virtual directory and with complete URL as other post mentions.

indiPy
  • 7,844
  • 3
  • 28
  • 39
0

You should consider using an Async Handler (ASP.NET) to download the file. Ultimately, the anchor with launch another request in the browser and that request will download the file to the user.

Here is an article with some info from MSDN.

Here is a code sample of what you would write in the handler:

public void ProcessRequest(HttpContext context) {
    // read input etx
    context.Response.Buffer = false;
    context.Response.ContentType = "text/plain";
    string path = @"c:\somefile.txt";
    FileInfo file = new FileInfo(path);
    int len = (int)file.Length, bytes;
    context.Response.AppendHeader("content-length", len.ToString());
    byte[] buffer = new byte[1024];
    Stream outStream = context.Response.OutputStream;
    using(Stream stream = File.OpenRead(path)) {
        while (len > 0 && (bytes =
            stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                outStream.Write(buffer, 0, bytes);
                len -= bytes;
            }
    }
}

Hope this helps

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73