Creating download link to a file on a file server

You can use ASHX file (say, downloadfile.ashx) and use the following code (not tested, but it will be something like that) in it:

 Response.Clear();
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment; filename=abc.txt");                                            
 Response.WriteFile(Server.MapPath("\\servername\folder1\folder2\folder3\abc.txt"));
 Response.End();

and then use this in your anchor tag like:

<a href="https://stackoverflow.com/questions/7725599/downloadfile.ashx"  target=""_blank"">Click me</a>

Note: You can also pass parameters for downloading different files like:

<a href="https://stackoverflow.com/questions/7725599/downloadfile.ashx?file=abc.txt"  target=""_blank"">Click me</a>

and then, in ashx file, use the file name to download the appropriate file.

Leave a Comment