Creating download link to a file on a file server

I’m looking for a way to (easily, by preference ;)) create a download link to a file on a separate file server.

The situation is as follows: the application I’m developing (asp.net 2.0 in vb.net but I have a similar issue in c#, either solution works for me) will be run internally for a company.
As is good practice, the file storage and web application are on two separate servers.

I basically need to be able to create a download link to a file, the only available URL i have to access the file is servernamefolder1folder2folder3file.txt (can be any sort of file)

Weblinks simply don’t work. This is how it’s currently set up:

tablerowfield.Text = String.Format(
    "<a href=""\servernamefolder1folder2folder3{0}" rel="nofollow noreferrer noopener"" 
        target=""_blank"">Click me</a>",
    filename)

Which doesn’t work for obvious reasons. It used to be set up to write that file to the application path itself and that worked perfectly, but it isn’t good practice and that’s why I’m changing it (or trying to).

I read solutions about creating a download page and then having a table in your DB which holds the links and returns the proper web URL for download but the time constraint I am faced with unfortunately doesn’t allow me to develop that.

Assuming I can provide a string with the full filepath to the file like the above, what is the easiest way to just create a link that, when clicked, downloads the document?

Note: I have 0 admin rights in this environment. That really isn’t helping me. Let’s assume I am given the correct link like above and have the appropriate file access rights and such.

UPDATE:

The above example does work in IE, but not in Firefox and Chrome. IE converts it to a file://servername/… link which does what it’s supposed to, but FF and Chrome both actively decided that this is unsafe and have disabled it from their browsers.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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("\servernamefolder1folder2folder3abc.txt"));
 Response.End();

and then use this in your anchor tag like:
<a href="downloadfile.ashx" rel="nofollow noreferrer noopener"  target=""_blank"">Click me</a>

Note: You can also pass parameters for downloading different files like:
<a href="downloadfile.ashx?file=abc.txt" rel="nofollow noreferrer noopener"  target=""_blank"">Click me</a>

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

Method 2

this piece of code will create a file in download folder with name=hi.txt and content as “thanks god, finally file got downloaded.”

 Response.Clear();
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment; filename=hi.txt");
 Response.Write("thanks god, finally file got downloaded.");
 Response.End();

Method 3

If your file is already there on server then you can use this code in your download button click event like this

protected void downloadpdf_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=""downloadName.pdf""");
    Response.WriteFile(Server.MapPath(@"~/path of pdf/actualfile.pdf"));
    Response.End();
}

Method 4

\servernamefolder1folder2folder3... is an UNC path which cannot be used from a browser. Because the file(s) are on a separate server, you need an href attribute of the form http://server-name/folder1/folder2/file.txt.

If the server-name is unresolvable by the clients, then you need to first get the IP address of the server and then formulate the href of the form: http://10.1.1.30/folder1/folder2/file.txt

Here is how you get the IP address from server-name:

IPAddress[] host;
host = Dns.GetHostAddresses("server-name");
string ip = host[0].ToString();

EDIT:

I basically need to be able to create a download link to a file

With the ashx solution your application would be reading the file from the server and relaying it to the clients rather than just providing the clients a link to download the file directly from the file server.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x