我正在使用ASP.NET来传输.jar文件。此代码完美适用于IE。但是,在Firefox上文件下载,损坏。解决问题的最佳方法是什么?以下是我正在使用的代码。在ASP.NET中,文件必须在Firefox中传输的方式有所不同吗?
private void TransferFile()
{
try
{
string filePath = Server.MapPath("SomeJarFIle.jar");
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
// Clear the content of the response
//Response.ClearContent();
Response.Clear();
// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(file.Extension.ToLower());
// Write the file into the response
//Response.TransmitFile(file.FullName);
Response.WriteFile(file.FullName);
// End the response
Response.End();
}
else
{
this.Response.Write("Error in finding file. Please try again.");
this.Response.Flush();
}
}
catch (Exception ex)
{
this.Response.Write(string.Format("Error: {0}", ex.Message));
}
}
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
case ".jar":
return "application/java-archive";
default:
return "application/octet-stream";
}
}
UPDATE:
我添加类型
case ".jar":
return "application/java-archive";
,但这并没有解决问题。如果我压缩了.jar文件,它可以很好地传输。
我没有注意到,当我再次测试我的本地主机文件下载没有问题。但是,当我把它推到网络服务器上的时候,我遇到了问题。
TransmitFile对更大文件的服务器更友善:http://www.ddj.com/windows/202804466 – Kev 2008-10-16 00:23:53
Kev - 非常感谢你为这个链接提供了很好的信息! – 2008-10-16 13:05:29