node.js - 将 .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 文件,但它已损坏。任何建议或想法
解决方案
推荐阅读
- c++ - 在 C++ 中处理文件期间的文件写入问题
- python - 创建numpy数组以仅包含其中列表的唯一元素的方法
- mysql - Sql中的替换函数获取重复的字符串
- unity3d - 使用rossharp与Hololens2和Ros通信,将Hololens2的摄像头图像发布为raw_image
- google-apps-script - 根据单元格值发送电子邮件
- javascript - clearInterval 不清除间隔
- python - 如何使用 QWindow 功能(例如 startSystemMove)和 QWidget 来制作自定义标题栏?
- c++ - 在 C++ 中查找 2 个数字的比率
- javascript - 通过附加脚本调用 javascritp window.onload api
- php - mysql中的Wordpress数据库连接问题