douyin6188 2019-05-12 20:27
浏览 722

从ajax响应中下载二进制文件

I have an Excel file which I upload using JavaScript. Once it is uploaded I perform some operations on it. Then I create a new Excel file from that data which is further downloaded.

Now everything is working fine, but the ajax response is returning a binary result of the output file. I want to download this file. The result of console.log is:

PK=�N%���a[Content_Types].xml͔]K�0���%��f� "�v��R���kX����׿�m��+����4�<�'��2�jgs6�,+��v����Sz���*a�����tr5^�=`Bb�9+c����,��9��.T"�kXr/�J,���[.��`ck6�?h�\��,*���ܠ}3�c�C+��9�-E��|c�j�BKPN�+�d��u��O1�
`o�Ba +���G�
�61yܑ{�y���ef�l�`�}���N�6=� �,�-m��ҷ.�ޝ[]��z�*�mע�x�GN���nN�J=YB��k�;��8~�����C�M5țax��?���1�'9n�(�"�z����o�������'PK=�N�78�K_rels/.rels���j�0��{
�{���1F�^ʠ�2��l�$���-}�y����Î��O��v�y�;�؋Ӱ.JP��޵^�����Yű�3G�Ww�g)���>�qQC��D���b!�]�i$L��3����2n���oT�:Z
�h����[��4�ი��]��yN�,ە�>�>�j
-'
V�)�#��EF^6��n���8q"K��H��>_ׄ����eƏ�<⇄�Ud�v��
T�PK=�Nx����xl/_rels/workbook.xml.rels���j�0��}
��X�ӆR"�R
���im�ؒ��?~��
m����;�1B���гw�sV@����*�;�
x��V��"I�e�,
1®��>c/)�D��Ȓ����<*�����h�M�� )��r/�A���<��0���̓������

$('#subb').on('click', function() {
    var file_data = $('#excel_upload').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    $.ajax({
        url: 'processing.php', // point to server-side PHP script 
        dataType: 'text',  // what to expect back from the PHP script, if anything
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success: function(data){
          var a = document.createElement('a');
            var url = data;
            a.href = url;
            a.download = 'myfile.pdf';
            a.click();
            window.URL.revokeObjectURL(url);
        }
     });
});

So any idea how to convert the binary file into excel and further download it

EDIT 1: So i solved it. Below is the javascript code i wrote

$('#submit_btn').on('click', function() {
    var file_data = $('#excel_upload').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    $.ajax({
        url: 'process.php', // point to server-side PHP script 
        dataType: 'json',  // what to expect back from the PHP script, if anything
        cache: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        beforeSend: function() {
        $("#submit_btn").html('<img src="https://thumbs.gfycat.com/UnitedSmartBinturong-max-1mb.gif" style="height:20px">');
        },
        success: function(data){
            var a = $("<a>");
            a.attr("href",data.file);
            $("body").append(a);
            a.attr("download","file.xls");
            a[0].click();
            a.remove(); 
            $("#submit_btn").html('Submit');
        }
        });

}); 

And at the server end

$writer->save('php://output');
$xlsData = ob_get_contents();
ob_end_clean();
$response=array('file'=>"data:application/vnd.msexcel;base64,"base64_encode($xlsData));
die(json_encode($response));

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置