2016-11-22 1 views
-1

En utilisant javascript. J'envoie une requête xhr à un serveur, donc je reçois une réponse xhr, qui contient une donnée en plusieurs parties, la deuxième partie contient un fichier téléchargeable (pdf, png, office)Comment lire les données binaires de la réponse xhr

dans la xhr.response ceci:

--_NextPart_000_0002_01C3E1CC.3BB37320 
Content-type: application/xml; charset=UTF-8 
<ns0:sendAttachmentOutput xmlns:ns0="http://****/webservices/definition/BDS/AttachmentFileStorage/sendAttachmentOutput/1"> 
    <ns0:msgCode>BDS0000</ns0:msgCode> 
    <ns0:msgLibelle>Pièce jointe envoyée</ns0:msgLibelle> 
</ns0:sendAttachmentOutput> 
--_NextPart_000_0002_01C3E1CC.3BB37320 
Content-disposition: attachment; filename="testsza.xlsx" 
Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 

PK  ! |lؖl  [Content_Types].xml (                                                                                                                              ]Kðǯ��4٦Ɉ۝珈Ω֦!'ܛ࠷4zԐ杷}Ҵ఼jLքZق��`KǴݕ�՟ҌôJg [email protected]]^ȫّ؅Õ1̡҈݇K3Ջ͌��̲.g ޞͨݍ`c[ 
6>@%&fϫ`ѥ��̠ӻċʔ,κ擯8uƚ̵ǫàÓ]��Ԃl"C|֍aȕ.͟ݛԮ˴ ʕˆv# ֠ѱ<ͼҚi蟙ŝ_>ң퟿�����$I爁`\3ж#z̹ցիԔgĮѤ8ϔ(N߅]d՝٧!Qþ4ڮࠞҒ鵃oןڼS:݅˗ѧ ��PK  ! ֕0#��L _rels/.rels (                                                                                                                                ̒ЎðǯH݃弪nHe܌HۡT$ͣ[email protected]��Êcܑ��[߮窔b/Nú(Aѳb{تx͟Vb"giǚΜaWޞl_xĔܢػȲˋڔ��OѰQˡhѓɥܔ彆ߞP-<ցj{ʾ״Mox/羢؎̀ޓ;̶愦ʏۨۂ̉Õʻ"cަۜO��q"KʐH᳼ߊ[email protected]쫁.ࠨʸގ<⧄⎤T_ ��PK  ! މ�� ԃ xl/_rels/workbook.xml.rels (                                                                ܓЪðǯýđ}qӮeԺ݌AЛ��㑄6׶'o?ԃۀɮWä��ȽᨯėjޕХ)ԥۨ̚<àֶҝӨ`@ÃqؿĎs$%Ǚ襤Ӡϩqmݔ.��궹֍ʼMײ��ƩΕð̶ NÏ 
--_NextPart_000_0002_01C3E1CC.3BB37320 

Je veux extraire les données binaires qui commencent à partir de PK.

ici est le code que j'ai écrit:

xhr.responseType = "arraybuffer"; 
     xhr.onload = function (e) { 
      var arraybuffer = xhr.response; 
      var fileArray = new Uint8Array(arraybuffer); 
var type = xhr.getResponseHeader('Content-Type'); 
      var boundary; 
      if (type.indexOf('boundary') != -1) { 
       boundary = type.substring(type.indexOf('boundary') + 9); 
      } 
      var temp = holder.split('--' + boundary); 
      var parts = []; 
      //loop through array to remove empty parts 
      for (var i = 0; i < temp.length; i++) { 
       if (temp[i] != "") { 
        parts.push(temp[i]); 
       } 
      } 

var type = parts[2].substring(parts[2].lastIndexOf('Content-type: ') + 14, parts[2].indexOf('\n', parts[2].lastIndexOf('Content-type: ')) - 1); 
       var filename = parts[2].substring(parts[2].indexOf('Content-disposition: attachment; filename="') + 43, parts[2].indexOf('\n', parts[2].indexOf('Content-disposition: attachment; filename="')) - 2); 
       var lastBoundary = holder.lastIndexOf(boundary) - 4; 
       //PARSE SECOND PART 

       //var fileStart = holder.indexOf('Content-disposition: attachment; filename="') + 43 + filename.length + 5; 

// ***** fileStart devrait avoir l'indice du début des données binaires, othehrwise il devrait commencer à partir de « PK », juste après la ligne de type de contenu

   //start point to the end of the array 
       var file = fileArray.buffer.slice(fileStart, lastBoundary); 

       if (!file || 0 === file.byteLength) { 
        _displayError("Pdf introuvable"); 
       } 
       else if (type == "text/html;charset=UTF-8") { 
        _displayError("Erreur de téléchargement du pdf. Veuillez contacter l'administrateur."); 
       } 
       else { 
        var blob = new Blob([file], 
        { 
         type: type 
        }); 

Comment puis-je faire cela?

+0

Etes-vous en train de télécharger ce qui est en fait le corps multipart qui sont souvent envoyés au serveur? – Endless

Répondre

0

J'ai trouvé la bonne expression de filestart:

var fileStart = holder.indexOf('Content-type: ', holder.indexOf('Content-disposition: ')) + type.length +18;