El envío de archivos a través de HttpContext hace que el navegador para colgar
-
23-08-2019 - |
Pregunta
Estoy enviando un archivo al usuario mediante un HttpHandler. En todos los navegadores, después de ver / descargar el archivo al menos una vez, el navegador / aplicación en posteriores visitas / descargas cuelga. Aquí está el código:
private void TransmitFile(HttpContext context, string filePath, string downloadName, bool forceDownload)
{
if (File.Exists(filePath))
{
string fileName = System.IO.Path.GetFileName(filePath);
string extension = Path.GetExtension(filePath);
FileInfo fileInfo = new FileInfo(filePath);
// set the response info/headers
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
if (forceDownload)
{
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadName.Replace(" ", "_") + extension);
context.Response.BufferOutput = false;
}
string type = "";
// set known types based on file extension
if (extension != null)
{
switch (extension.ToLower())
{
case ".tif":
case ".tiff":
type = "image/tiff";
break;
case ".jpg":
case ".jpeg":
type = "image/jpeg";
break;
case ".gif":
type = "image/gif";
break;
case ".doc":
case ".rtf":
type = "Application/msword";
break;
case "pdf":
type = "Application/pdf";
break;
case "png":
type = "image/png";
break;
case "bmp":
type = "image/bmp";
break;
default:
type = "application/octet-stream";
break;
}
}
context.Response.ContentType = type;
context.Response.TransmitFile(filePath);
context.Response.Flush();
}
else
{
Immersive.Diagnostics.Log.Warn("Requested file does not exist: " + filePath, this);
Immersive.Diagnostics.Log.Warn("", this);
}
}
He leído que Response.Close llamando al () y Response.End () no es una buena idea? Han intentado tanto dejando en, y la eliminación, y todavía ocurre.
EDIT:
Parece que TransmitFile ha conocido problemas. Una explicación más detallada se puede encontrar en: http: // www. improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application
He quitado TransmitFile y cambió de escritura de archivo, y ahora funciona perfectamente.
context.Response.WriteFile(filePath);
context.Response.Flush();
context.Response.Close();
Solución
Esto puede ser un problema conocido si el servidor que está descargando desde ejecuta Windows Server 2003 SP1.
Aquí está una revisión: http://support.microsoft.com/kb/902780
Además, compruebe si ha OutputCache
habilitado en la página, si es así, intente de nuevo la descarga sin él.
Otros consejos
algún motivo usted no está utilizando el búfer? Eres Flush () ing que a pesar de que no lo esté utilizando. Y sí, creo que también debe hacer una Response.End () después de la Escalera ().
Quiero saber si esto funciona y tal vez pueda hacer algunas pruebas de mi propia.