我目前有一个.NET http处理程序,用于处理将文件传递到Web浏览器以供下载。 IE6 +,FireFox,Chrome和Safari都使用此代码,但新的IE9无法下载,但仅限于SSL。
当我点击链接下载文件时:
https://rootwebsite/taskmanager/DownloadFiles.ashx?fileId=3b2c7e41-f51a-445d-9627-f4f4481e1425
IE9打开保存对话框并显示DownloadFiles_ashx?fileId=3b2c7e41-f51a-445d-9627-f4f4481e1425
作为文件名,但拒绝下载文件。
如果我将链接更改为http://
,则代码正常,文件将下载。
区别是什么?我错过了什么?
这是我的代码:
public void WriteByteArrayToHttp(HttpResponse response, string fileName, string contentType, Stream file, bool downloadFile)
{
using (file)
{
if (downloadFile)
{
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.AddHeader("Content-Disposition",
string.Format("attachment; filename={0}", HttpUtility.UrlEncode(fileName)));
}
response.AddHeader("Content-Length", file.Length.ToString());
// Added with suggestion of YSlow FireFox plug-in
// Specifies how long the file is valid for in cache in seconds
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
// See 14.9 Cache-Control
// 6 Hours
response.AddHeader("max-age", "21600");
response.ContentType = contentType;
// At the time of this writing, we are running IIS6, BUT if we decide to go to IIS7
// there is a 28.6MB limit to content size both up and down by default
// See http://msdn.microsoft.com/en-us/library/ms689462.aspx
// This would be a problem for a number of files we serv up with the Original method
// so this chunking method replaces that.
// See http://support.microsoft.com/kb/812406 as the base for this change.
// Tested with file between 1MB and 3GB
// Total bytes to read:
long dataToRead = file.Length;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
try
{
while (dataToRead > 0)
{
// Verify that the client is connected.
if (response.IsClientConnected)
{
// Read the data in buffer.
// Length of the file:
int length = file.Read(buffer, 0, 10000);
// Write the data to the current output stream.
response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (HttpException hex)
{
if (!hex.Message.StartsWith("The remote host closed the connection"))
{
throw;
}
}
}
}
web.config将处理程序定义为:
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add name="DownloadFiles.ashx" path="DownloadFiles.ashx" verb="*" type="Russound.Web.HttpHandlers.DownloadFileHandler" resourceType="Unspecified" preCondition="integratedMode"/>
</handlers>
</system.webServer>
</configuration>
答案 0 :(得分:3)
可能与
有关昨天让我头疼(这是我的第二个)。
如果你问我,这个Internet Explorer选项Do not save encrypted pages to disk
应该只适用于缓存而不是故意下载......但是,嘿,这是微软我们正在谈论的,所以它不需要无论如何都要有任何意义。
2011年2月更新:我修改了 IE9的文件下载逻辑。 IE9 应该能够成功 无论HTTPS还是HTTPS,都可以下载文件 除非你有,否则Cache-Control标头 “不要将加密的页面保存到 磁盘“选项集。