如何从xhr响应中读取二进制数据

时间:2016-11-22 18:05:23

标签: javascript binaryfiles tibco-business-works

使用javascript。 我正在向服务器发送xhr请求,因此我收到xhr响应,其中包含多部分数据,第二部分包含可下载文件(pdf,png,office文档)

在xhr.response中我得到了这个:

<route>
    <pattern>/test_pattern</pattern>
    <implicit-parameter name="action">testAction</implicit-parameter>

    <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
    <implicit-parameter name="p_p_id">xxx_WAR_xxxportlet_INSTANCE_73iYU2pK0li</implicit-parameter>
    <implicit-parameter name="p_p_state">normal</implicit-parameter>
    <implicit-parameter name="p_p_mode">view</implicit-parameter>
    <implicit-parameter name="p_p_col_id">column-1</implicit-parameter>
    <implicit-parameter name="p_p_col_count">1</implicit-parameter>
</route>

我想提取从PK开始的二进制数据。

这是我写的代码:

--_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 k@6]^ȫّ؅Õ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$ͣ$@��Êcܑ��[߮窔b/Nú(Aѳb{تx͟Vb"giǚΜaWޞl_xĔܢػȲˋڔ��OѰQˡhѓɥܔ彆ߞP-<ցj{ʾ״Mox/羢؎̀ޓ;̶愦ʏۨۂ̉Õʻ"cަۜO��q"KʐH᳼ߊs@쫁.ࠨʸގ<⧄⎤T_   ��PK     ! މ��  ԃ   xl/_rels/workbook.xml.rels (                                                                                                                                                                                                                                                                 ܓЪðǯýđ}qӮeԺ݌AЛ��㑄6׶'o?ԃۀɮWä��ȽᨯėjޕХ)ԥۨ̚<àֶҝӨ`@ÃqؿĎs$%Ǚ襤Ӡϩqmݔ.��궹֍ʼMײ��ƩΕð̶ NÏ
--_NextPart_000_0002_01C3E1CC.3BB37320

// ***** fileStart应该具有二进制数据开头的索引,其他应该从'PK'开始,就在内容类型行之后

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;

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我找到了正确的文件启动表达式:

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