我使用ASP.NET传输.jar文件。此代码在IE上完美运行。但是在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";
}
}
更新:
我添加了类型
case ".jar":
return "application/java-archive";
这并没有解决问题。如果我压缩.jar文件,它就可以正常传输。
我确实注意到当我再次测试我的localhost文件被下载时没有任何问题。但是当我把它推到网络服务器上时就是我遇到问题的时候。
答案 0 :(得分:4)
我没有在ReturnExtension()函数中看到“.jar”的情况(我认为最好将其命名为“ReturnMimetype”)。这可能是问题,还是你忘记将其粘贴?
.jar的mimetype应该是 application / java-archive 。详情请见http://en.wikipedia.org/wiki/Jar-file
我认为这是问题所在。我记得在传输.docx文件时遇到了同样的问题(实际上是一个带有不同扩展名的zip文件,作为.jar文件)。下载在IE中运行良好,但Firefox损坏了它。解决方案是发送正确的mimetype。
答案 1 :(得分:1)
我只看到两件事,看起来你没有.jar文件扩展名的mime类型。
我个人使用writefile而不是传输,但我不确定区别。
答案 2 :(得分:1)
使用Response.WriteFile或Response.BinaryWrite,TransmitFile方法有一些已知的奇怪行为。