2008-10-15 65 views
1

我正在使用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文件,它可以很好地传输。

我没有注意到,当我再次测试我的本地主机文件下载没有问题。但是,当我把它推到网络服务器上的时候,我遇到了问题。

回答

4

我没有在ReturnExtension()函数中看到“.jar”的情况(我认为它可能更好,名为“ReturnMimetype”)。这可能是问题,还是你忘了粘贴它?

.jar的mime类型应该是application/java-archive。详情在这里:http://en.wikipedia.org/wiki/Jar-file

我认为这是问题。我记得当我传输一个.docx文件(实际上是一个具有不同扩展名的zip文件,如.jar文件)时,会出现同样类型的问题。下载在IE中运行良好,但Firefox损坏了它。解决方案是发送正确的mimetype。

1

我只看到两件事情,它没有出现你有.jar文件扩展名的MIME类型。

两个我个人使用writefile而不是传输,但我不确定区别。

+0

TransmitFile对更大文件的服务器更友善:http://www.ddj.com/windows/202804466 – Kev 2008-10-16 00:23:53

+0

Kev - 非常感谢你为这个链接提供了很好的信息! – 2008-10-16 13:05:29

1

使用Response.WriteFile或Response.BinaryWrite,使用TransmitFile方法存在一些已知的奇怪行为。

相关问题