Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file from a link

I have a web page using C# where I want users to be able to click a link (or a linkbutton or a button, I'm not fussy) and have the "Save As" dialog window appear so they can download the file. The file itself is located on another server so I have to use the absolute path (i:\division\department\publicfiles\filename.pot). Does anyone know how to do that?

I looked up the question here and some people suggested webClient.DownloadFile. Except I can't use that because it requires that you already know where the user wants the file to be downloaded to on their computer. Basically what I'm looking for is what happens when you right click on a link and select "save as", but done when you left click on a link.

Thank you

like image 918
Alverant Avatar asked Jul 19 '12 14:07

Alverant


2 Answers

"The file itself is located on another server" to me sounds like it might not be published to the web and thus doesn't have a URL. Therefore a simple anchor tag will not work in this case. I think you're looking for Response.TransmitFile().

When they click on the link, you need to send the file and explicitly set the content type and content-disposition header. Setting the content-disposition header to attachment will pop up the save as dialog. Like (untested):

Response.ContentType = "application/x-pot";    
Response.AppendHeader("Content-Disposition","attachment; filename=filename.pot");    
Response.TransmitFile(@"i:\division\department\publicfiles\filename.pot");    
Response.End();
like image 122
lc. Avatar answered Oct 02 '22 16:10

lc.


You can do it like this,

<a href="http://www.yoursite.com/folders/yourfile.pdf"  target="_blank" > click to download file </a>
like image 21
Adil Avatar answered Oct 02 '22 17:10

Adil