2008-10-15 2 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에 대한 mimetype은 application/java-archive입니다. 여기에 세부 사항 : http://en.wikipedia.org/wiki/Jar-file

나는 이것이 문제라고 생각한다. .docx 파일 (실제로는 .jar 파일과 같이 다른 확장자를 가진 zip 파일)을 전송할 때 동일한 유형의 문제가 있음을 기억합니다. IE에서 다운로드가 제대로 작동했지만 Firefox가 손상되었습니다. 솔루션은 정확한 MIME 형식을 전송하는 것이 었습니다.

1

.jar 파일 확장명에 대한 MIME 유형이없는 것으로 보이는 것은 두 가지뿐입니다.

2 나는 개인적으로 전송보다는 writefile을 사용하지만 차이점을 잘 모르겠습니다.

+0

TransmitFile은 더 큰 파일을 가진 서버에 친절합니다 : http://www.ddj.com/windows/202804466 – Kev

+0

Kev - 그 링크에 대한 정보를 매우 고맙습니다! –

1

Response.WriteFile 또는 Response.BinaryWrite를 사용하면 TransmitFile 메서드에 몇 가지 이상한 동작이 있습니다.

+0

알려진 이상 동작을 문서화 할 수있는 링크가있을 수 있습니까? 빠른 Google 검색을 수행해도 관련성이 없었습니다. – ckittel

+0

@ckittel - 아직 적용되는지 모르겠다 - 잠시 뒤였습니다. 많은 검색의 긴 꼬리 부분에서 그것을 찾았을 때만 기억할 수 있었고 비슷한 문제는 TransmitFile을 버림으로써 수정되었습니다. – seanb

관련 문제