首页 > 解决方案 > 将 .zip 文件从服务发送到 nodejs 应用程序,然后再发送到浏览器

问题描述

我正在尝试将 zip 文件从服务器(A)发送到服务器(B)

浏览器

$.ajax({
        url: 'routes/getFile',
        method: "POST",
        contentType: 'application/json',
        success: function (data) {
                 var byteCharacters = data;
                 var byteNumbers = new Array(byteCharacters.length);
                 for (var i = 0; i < byteCharacters.length; i++) {
                        byteNumbers[i] = byteCharacters.charCodeAt(i);
                 } 
                 var byteArray = new Uint8Array(byteNumbers);
                 var blob1 = new Blob([byteArray], {type: "application/zip, application/octet-stream"});
                 var fileName1 = "cool.zip";
                 saveAs(blob1, "my.zip");
 }

服务器 B(请求 zip 文件)

router.post('/getFile', (req, res, next) => { 
var options = {
    uri: 'Server_A_location'+ 'get_Zip',
    method: 'POST,
    json: req.body       
  }
 rp(options).then(function (data) {
     console.log(data)
     var buff = Buffer.from(data);
     res.send(buff);
 }

服务器 A(这是 zip 文件)

    var admzip=require('adm-zip')
    router.post('/get_Zip',function(req,res,next){
       var zip=new admZip();
       zip.addLocalFile('..path_of_zip_file');
       var zip_contents=zip.toBuffer();
       res.writeHead(200,{
                    'Content-Disposition':'attachment; filename="myfile.zip",
                    'Content-Type':'application/zip'
       })
       return res.end(zip_contents);

我在 ServerB 收到的数据是二进制格式:PKYUQ�3�test1/test1.1.json�}kw⸲���+...

在浏览器中,我得到了 zip 文件,但它已损坏。任何建议或想法

标签: node.jsweb-servicesbinaryzipbuffer

解决方案


推荐阅读