2011-11-25 3 views
1

아래와 같은 문제가 있습니다 : 스트림 파일을 읽을 수있는 SOAP 웹 서비스가 있습니다. 나는 청크로 분할 된 전체 파일을 읽고 사용자에게 전송해야합니다. 모든 작업은 UI 메인 스레드를 차단하지 않아야합니다 : 사용자가 파일 저장 대화 상자에서 '저장'버튼을 누르고 다음 페이지로 이동하거나 다른 작업을 수행 할 수 있습니다. 샘플 솔루션에 대해 고맙게 생각합니다. 이 솔루션은 IIS 5.1에서 작동해야합니다. 응답 페이지로 ASP.NET에서 바이트 단위를 파일을 다운로드청크 파일 다운로드

감사합니다, 지미

+4

기억하십시오 : 사람들은 당신을 위해 당신의 일을하지는 않을 것입니다.하지만 그들은 기꺼이 도와 드리겠습니다. 따라서 생각하고있는 솔루션, 구현하려고 시도한 것이 무엇이며 왜 작동하지 않는지에 대해 설명하면 유용한 응답을받을 가능성이 훨씬 더 큽니다. – Rune

+0

나는 누군가가 나를 위해 그것을 쓸 것이라고 기대하지 않는다. 유감스럽게도 법적인 이유로 기존 솔루션을 표시 할 수 없습니다. 모든 팁이나 의사 코드는 매우 유용합니다. – grom

+0

우리는 100KB 제작 소스 파일을 요구하지 않고, 당신이 해결하고있는 작업을 보여주는 스 니펫 만 필요합니다. 아니면 너에게 불법이라고 생각하니? 귀하의 법적 의무는 Stack Overflow에 관한 질문을 게시 할 수 있습니까? :) – Zruty

답변

2

. 이것에 대해 msdn에서 확인 :

try 
    { 
     System.String filename = "C:\\downloadJSP\\myFile.txt"; 

     // set the http content type to "APPLICATION/OCTET-STREAM 
     Response.ContentType = "APPLICATION/OCTET-STREAM"; 

     // initialize the http content-disposition header to 
     // indicate a file attachment with the default filename 
     // "myFile.txt" 
     System.String disHeader = "Attachment; 
     Filename=\"myFile.txt\""; 
     Response.AppendHeader("Content-Disposition", disHeader); 

     // transfer the file byte-by-byte to the response object 
     System.IO.FileInfo fileToDownload = new 
     System.IO.FileInfo(filename); 
     System.IO.FileStream fileInputStream = new 
     System.IO.FileStream(fileToDownload.FullName, 
     System.IO.FileMode.Open, System.IO.FileAccess.Read); 
     int i; 
     while ((i = fileInputStream.ReadByte()) != - 1) 
     { 
     Response.Write((char)i); 
     } 
     fileInputStream.Close(); 
     Response.Flush(); 
     Response.Close(); 
    } 
    catch (System.Exception e) 
    // file IO errors 
    { 
     SupportClass.WriteStackTrace(e, Console.Error); 
    } 

당신이 오류를 구현하고 해결하는 데 도움이 수있는 몇 가지 기사가 있습니다

download an excel file from byte() on https server

asp.net downloading file from ftp getting it as byte[] then saving it as file

Remote file Download via ASP.NET corrupted file

Response.WriteFile cannot download a large file

0

이 ProcessRequest 방법 양식 다운 로더 HttpHandler를 :

public void ProcessRequest(HttpContext context) 
     { 
      RequestTarget target = RequestTarget.ParseFromQueryString(context.Request.QueryString); 
      Guid requestId = new Guid(context.Request.QueryString["requestId"]); 
      string itemName = HttpUtility.UrlDecode(context.Request.QueryString["itemName"]); 
      if (target != null && 
       !requestId.Equals(Guid.Empty) && 
       !string.IsNullOrEmpty(itemName)) 
      { 
       HttpResponse response = context.Response; 
       response.Buffer = false; 
       response.Clear(); 
       response.AddHeader("Content-Disposition", "attachment;filename=\"" + itemName + "\""); 
       response.ContentType = "application/octet-stream"; 
       int length = 100000, i = 0; 
       byte[] fileBytes; 
       do 
       { 
        fileBytes = WS.ReadFile(requestId, target, i * length, length); 
        i++; 
        response.OutputStream.Write(fileBytes, 0, fileBytes.Length); 
        response.Flush(); 
       } 
       while (fileBytes != null && fileBytes.Length == length); 
      } 
     } 

전체 문제 다운로드 동작을 구성하지만 작업이 UI 메인 스레드를 차단하지 않습니다한다 다운로드 조건이 만족되지 않습니다 : 파일을 저장 한 사용자 프레스 '저장'버튼을 대화 상자를 열고 다음 페이지로 이동하거나 다른 작업을 수행 할 수 있습니다. Niranjan Kala에 의해 작성된 솔루션은 파일이 매우 큰 경우 사용자가 다운로드 작업이 완료 될 때까지 다른 페이지를 볼 수 없습니다. 고맙겠지 만 그것이 의미하는 바가 아닙니다 ...

0

정확하게 이해하면 브라우저가 현재 페이지를 다시로드하지 않고도 파일에 대한 새 요청을 시작하게하려는 경우입니다. 가장 쉬운 방법은 target = "_ blank"와 링크를 만드는 것입니다. 이런 식으로 뭔가를 수행해야합니다

<a href="http://yourhost/yourdownloadhandler?requestid=blah&itemName=blahblah" target="_blank">Download file</a> 

당신이 파일을 디스크에 저장됩니다 application/octet-stream 대부분의 브라우저의 콘텐츠 형식을 제공합니다.