Javascript – How to force a hyperlink to show the open/save dialog box instead of opening it in browser

asp.nethtmljavascript

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.

Best Answer

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

response.setHeader("Content-Disposition: attachment", "filename=\"" + filePath + "\"");