Downloading a file from a link

14,313

Solution 1

"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();

Solution 2

You can do it like this,

<a href="http://www.yoursite.com/folders/yourfile.pdf"  target="_blank" > click to download file </a>
Share:
14,313
Alverant
Author by

Alverant

Updated on June 06, 2022

Comments

  • Alverant
    Alverant almost 2 years

    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