You can't force a file download from a hyperlink alone (or reliably using Javascript) unless you add the Content-Disposition header to the response; therefore, one way to address this issue and always force a file download could be to have an intermediate page that will add the header for you.
Your link, therefore, has to become something like this:
<a href="DownloadMgr.aspx?File=brochure.pdf" target="_blank">Download Brochure</a>
And DownloadMgr.aspx
should be something like this:
if(!IsPostback)
{
if(Request.QueryString["File"]!=null)
{
if(Request.QueryString["File"].Contains("pdf")))
Response.ContentType = "application/pdf"; //varies depending on the file being streamed
Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"]);
Response.WriteFile(Server.MapPath(Request.QueryString["File"]));
}
A better approach, though, is to create an HTTPHandler to do the same thing. You can look at this answer for more details on how to do it. One of the benefits of creating an HTTPHandler is that it doesn't involve all the processing, initialization and so forth, that's required for a regular aspx
page.
Sample with full code:
<%@ Language=C# %>
<HTML>
<head>
<title>Download Manager</title>
</head>
<script runat="server" language="C#">
void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
if (Request.QueryString["File"] != null)
{
if (Request.QueryString["File"].Contains("pdf"))
Response.ContentType = "application/pdf"; //varies depending on the file being streamed
Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"]);
Response.WriteFile(Server.MapPath(Request.QueryString["File"]));
}
}
}
</script>
<body>
<form id="form" runat="server">
</form>
</body>
</HTML>
VB.NET version
<%@ Language="VB" %>
<HTML>
<head>
<title>Download Manager</title>
</head>
<script runat="server" language="VB">
Sub Page_Load()
If (Not Page.IsPostBack) Then
If (Request.QueryString("File") IsNot Nothing) Then
If (Request.QueryString("File").Contains("pdf")) Then
Response.ContentType = "application/pdf" 'varies depending on the file being streamed
End If
Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString("File"))
Response.WriteFile(Server.MapPath(Request.QueryString("File")))
End If
End If
End Sub
</script>
<body>
<form id="form" runat="server">
</form>
</body>
</HTML>
Now save the above code as DownloadMgr.aspx
and drop it inside your website.