2012-03-20 11 views
2

AJAX 호출로 서버에서 파일을 다운로드하고 싶습니다. 그것은 다음과 같이 iframe를 사용하여 쉽게 수행 할 수 있습니다XMLHttpRequest에서 나오는 옥텟 스트림을 처리하는 방법

var ifrm = document.getElementById("fileframe"); 
ifrm.src = "DownloadFile?fileid="+fileid; 

하지만 더 복잡한 문제가 있습니다. 경우에 따라 application/octet-stream 대신 'text/html'이라는 콘텐츠 유형의 응답이 백엔드에서 제공됩니다.

XMLHttpRequest을 사용하여 다운로드하는 파일을 백업합니다. 을 사용하여 XMLHttpRequest 응용 프로그램/옥텟 스트림을 처리하기 위해 파일 저장 대화 상자가 팝업되도록 브라우저를 만들 수 있습니까?

// connect function handles the XMLHttpRequest communication with the backend 
var connection = connect('DownloadFile','fileid='+fileid); 
connection.onreadystatechange = function(){ 
    if(connection.readyState == 4) { 
     var contentType = connection.getResponseHeader('Content-Type'); 
     if(contentType == 'application/octet-stream'){ 
      // here I want to make the browser popup a save file dialog 
      // for the incoming octet stream 
      // maybe I can direct stream to an iframe, but how?? 

     }else{ 
      alert(connection.responseText); 
     } 
    } 
    if((connection.readyState == 2)||(connection.readyState == 3)){ 
    } 
} 

답변

0

두 가지 옵션을 생각해 볼 수 있습니다.

  1. 먼저 HEAD 요청을 보내고 콘텐츠 유형에 따라 동작을 결정하십시오.
    기대하는 바를 알고 GET 요청을 발행하십시오.
  2. 데이터로 URI (RFC 2387)를 만들고 window.open을 사용하여 엽니 다.
    브라우저는 렌더링 할 수없는 콘텐츠에 대한 대화 상자를 엽니 다.
관련 문제