2

In my code I am generating several hyperlinks dynamically which are direct links to PDF/Jpg/bitmap/Png/Docx/Xlsx/Pptx files hosted in the server.

the code looks somewhat like this.

private void PopulateLinks(string linkText, string URL)
{
   DIV_download.innerHtml += "<a href='" + URL + "'>" + linkText + "</a> <br/>"; 
}

The problem is whatever files browser can handle, it is opening in the same tab. is there any way to force the download dialog box for PDF and JPEG/Bitmap/Png files?

Since I need this in client side, I can not use the content-dispostion way, can it be done using javascript or any other markup? My clients only use internet explorer so it will be enough for me if it works only in IE.

Manas Saha
  • 1,477
  • 9
  • 29
  • 44
  • What do you mean you need this client side? Can you modify the .htaccess file? – mowwwalker Mar 28 '12 at 04:56
  • possible duplicate of [Force PNG to download instead of opening in browser with IIS](http://stackoverflow.com/questions/1040328/force-png-to-download-instead-of-opening-in-browser-with-iis) – Ian Mercer Mar 28 '12 at 05:01
  • I'm struggling with this issue too, trying to open a Save dialog for a PDF file. Is there anyway to do this using client-side JavaScript only and no modifications on the server side? – shabdar Jul 27 '12 at 23:10

3 Answers3

3

try Content-Disposition: attachment property in your code example of my code:

response.setHeader("Content-Disposition: attachment", "filename=\"" + filePath + "\"");
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
m.mehta
  • 31
  • 2
0

I'm pretty sure you cannot do this client side. However you could add a target=_blank attribute to the link tag. That will force it to open in a new window. Or you could have your client right click the link and Save Target As.

Jeff
  • 340
  • 1
  • 4
  • 16
0

You can do this in IE by using the DOM method document.execCommand():

<a href="javascript:void(0);"
 onclick="document.execCommand('SaveAs',true,'file.html');"
 >Save this page</a> 

This if from http://www.jtricks.com/bits/content_disposition.html. You will probably have to investigate cross-browser compatibility.

There is also information in this question that may be of interest for working with non-IE browsers: Cross-browser Save As .txt

Community
  • 1
  • 1
Mike Chamberlain
  • 39,692
  • 27
  • 110
  • 158