В настоящее время у меня есть обработчик .NET http, который обрабатывает передачу файлов в веб-браузеры для загрузки. 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>