2012-01-09 6 views
1

인덱스와 다운로드 페이지가 두 개있는 컨트롤러가 있습니다. 다운로드를 클릭하면 서비스에서 바이트 []가 검색되고 Response.BinaryWrite를 사용하여 저장합니다 파일. 이 문제는 Response.Clear를 사용하면 다운로드 페이지가 렌더링에서 중지되지만 파일을 성공적으로 다운로드한다는 점에서 문제가 있습니다. 파일을 다운로드하고 페이지를 렌더링 할 수있는 방법이 있습니까? 내가 .NET 4를 사용하고방법 : 렌더링에서 페이지를 멈추지 않고 웹 서비스에서 파일 저장하기

가, ASP, 모노레일 성, C#을

내가 MVC를 사용하여 내 의견에 대한 반환 형식으로 ActionResult 및 FileResult를 사용 할 수 있음을 알고, 나는 그러나 사용하도록 제한하고 모노레일 성 (Monorail Castle)은 기존 프로젝트이기 때문에 최근에 소유권을 쥐어 봤고 아직 기술 변경에 무게를 두지 않았습니다. 다음은

다운로드 페이지

<table> 
    <tr> 
     <td>Your file has been downloaded successfully</td> 
    </tr> 
</table> 

답변

1

내가 생각 해낸 대답은 같은 일반적인 아니라, 아직이 솔루션은 작동 : 대안 들어,이 질문을 참조하십시오. 여기

public class MyController : Controller 
{ 
    public void Index() 
    { 
     PropertyBag["includeZeroBalances"] = false; 
     PropertyBag["toDate"] = DateTime.Today.ToShortDateString(); 
    } 

    public void Download(bool includeZeroBalances, DateTime toDate) 
    { 
     MyProxy proxy = GetProxy(); 
     byte[] file = proxy.GetFile(includeZeroBalance, toDate); 
     PropertyBag["downloadurl"] = "data:application/zip;base64," + System.Convert.ToBase64String(file); 
    } 
} 

다음은 인덱스 페이지

${Form.FormTag({@action: 'Download'})} 
    <table> 
     <tr> 
      <td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td> 
      <td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td> 
     </tr> 
     <tr> 
      <td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td> 
      <td>${Form.CheckboxField("includeZeroBalances")}</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>${Form.Submit("Download", {@class: 'submitb'})}</td> 
     </tr> 
    </table> 
${Form.EndFormTag()} 

입니다 또한 새 창에서 파일을 다운로드 자동화하기 위해 자바 스크립트를 추가 할 수 있습니다 다운로드 페이지

<table> 
    <tr> 
     <td>Your file has been generated successfully. <a href="${downloadurl}">Click here</a> to download your file</td> 
    </tr> 
</table> 

입니다. 파일을 저장하려면이 방법을 사용

document.Ready(function() 
{ 
    window.open("${downloadurl}"); 
}); 

장점 : 어떤 파일이 서버에 저장되지 않습니다. 약 300kb 이상의 파일을 다운로드 할 때 오버 헤드가 약간 낮아집니다. 응답 데이터를 덮어 쓰지 않으므로보기가 정상적으로 렌더링됩니다.

이 방법을 사용하여 파일을 저장하는 데 따른 단점 : 모든 버전의 브라우저에서 사용하기에 충분하지 않습니다. 정적 파일에 사용하는 경우 파일이 변경되면 파일 내용이 uri에 포함되어 있기 때문에 다시 생성해야합니다.

이 문제로 인해 다른 사람들에게 도움이되기를 바랍니다.나는 직설적 인 해결책이없는 수많은 게시판에서 이런 유형의 질문을 보았습니다.

1

될이 작업을 수행 할 수있는 '권리'방식으로 인덱스 페이지 여기

${Form.FormTag({@action: 'Download'})} 
    <table> 
     <tr> 
      <td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td> 
      <td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td> 
     </tr> 
     <tr> 
      <td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td> 
      <td>${Form.CheckboxField("includeZeroBalances")}</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>${Form.Submit("Download", {@class: 'submitb'})}</td> 
     </tr> 
    </table> 
${Form.EndFormTag()} 

내 예제 코드 여기

public class MyController : Controller 
{ 
    public void Index() 
    { 
     PropertyBag["includeZeroBalances"] = false; 
     PropertyBag["toDate"] = DateTime.Today.ToShortDateString(); 
    } 

    public void Download(bool includeZeroBalances, DateTime toDate) 
    { 
     MyProxy proxy = GetProxy(); 
     byte[] file = proxy.GetFile(includeZeroBalance, toDate); 
     Response.Clear(); 
     Response.ContentType = "application/zip"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename="TestFileName.zip"); 
     Response.BinaryWrite(file); 
    } 
} 

임 불행히도 복수 응답을 사용하여 some browsers don't support them.

당신이 할 수있는 일은 sourceforge 및 다른 사이트처럼 메타 새로 고침을 사용하는 것입니다. 내가 좋아하는 것 같은

+0

안녕하세요, Mauricio, 제공하신 링크와 통찰력에 감사드립니다. 불행히도, 이것은 제가 찾던 것이 전부가 아닙니다. –

+0

@ROFFELPOMP : 글쎄, 네 대답에 정확히 무엇을 했나. –