I have a simple script which opens an new window using javascript :
<script type="text/javascript">
window.open("/test/test.aspx", "", "", "");
</script>
This works great when dealing with normal webpages, but when I try to download pdf file using :
context.Response.ContentType = "application/pdf";
context.Response.WriteFile(fileFullName);
I can see the window opening but it closes directly afterwards. This behavior is only shown when not working in local, when accessing a different server.
When using different browser thant IE8, there are no problem, the download works perfectly.
I have for the moment find a work around opening a page with a link in it, so that the user click and access his download, but I do not like the solution so much.
thanks for your inputs,
[EDIT] Here is a simplified code :
page1 :
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
window.open("/page2.aspx", "", "", "");
</script>
</div>开发者_如何学JAVA;
</form>
</body>
</html>
page 2
protected void Page_Load(object sender, EventArgs e)
{
string fileFullName = ApplicationStoringPath.TempReportPath + "00329bad-28ac-46bd-9f0a-3eced660f079.pdf";
string fileName = "test.pdf" ;
if (File.Exists(fileFullName))
{
FileStream file = File.Open(fileFullName, FileMode.Open);
long length = file.Length;
file.Dispose();
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.AddHeader("Content-Length", length.ToString());
Response.ContentType = "application/pdf";
Response.WriteFile(fileFullName);
}
}
Here what happens :
Accessing page 1 from local opens popup and proposes the download. (IE) Accessing page 2 from local proposes the download.(IE)
Accessing page 1 from different PC opens popup and does not propose the download. (IE) Accessing page 2 from different PC proposes the download. (IE)
Accessing page 1 from different PC opens popup and proposes the download. (FireFox) Accessing page 2 from different PC proposes the download. (FireFox)
This is not some popup blocker situation. The file that I try to access is located in the temp folder of the site that host my pages, ie the same server.
[EDIT]
In my case file was downloading but open-save pop-up was not showing. I just enabled 'Automatic prompting for file download' and it worked :)
Open Internet Explorer > Tools > Internet Options > Security > Custom level > Download : Select Enable for 'Automatic prompting for file download'.
Could it be you have a Popup blocker on IE8 that is not configured yet?
Hi all thanks for your help.
Whatever I tried using a popup did not succeed and therefore I tried using an iframe to make my download. Luckily it worked like a charm.
same code then but I now use :
<iframe id="DownloadIFrame" visible=false height=1 width=1></iframe>
<script type="text/javascript">
document.getElementById('DownloadIFrame').src = "/page2.aspx";
</script>
精彩评论