简体   繁体   English

可以将响应设置为ArrayBuffer客户端,但不能使用Node.js

[英]Can Set Response as ArrayBuffer Client Side but not using Node.js

I have a client side form using XMLHTTPResponse that allows me to get response data saved as a file. 我有一个使用XMLHTTPResponse的客户端表单,该表单允许我获取保存为文件的响应数据。 It sets the response type as arraybuffer as follows before the rest of the blob conversion occurs: 在其余的blob转换发生之前,它将响应类型设置为arraybuffer,如下所示:

xhr.responseType = "arraybuffer";

I have been searching and finding multiple methods to create an arraybuffer, but none that detail how to take the response in node to an arraybuffer. 我一直在寻找并找到创建数组缓冲区的多种方法,但是没有一个详细说明如何将节点中的响应传递给数组缓冲区。 I am using unirest as follows: 我正在使用unirest,如下所示:

unirest.post('http://myvendorsapi/Upload_PDF')
  .headers({ 'Content-Type': 'multipart/form-data' })
  .field('filename', filename)// Form field
  .attach('file', fileloc)// Attachment
  .end(function (response) {
     console.log(response);
     var returnfile = response.body;
     // Need logic to convert to arraybuffer
  });

How do I set the response type to an arraybuffer or convert my response to an arraybuffer? 如何将响应类型设置为arraybuffer或将响应转换为arraybuffer?

If you want to get more raw response data, I would ditch the unirest library and use something thinner such as request . 如果您想获取更多原始响应数据,我将放弃unirest库,并使用更薄的东西(例如request) Unirest claims to 'parse responses' for you, which sounds like something that you don't want. Unirest声称可以为您“解析响应”,这听起来像您不想要的东西。 If you simply want to save the raw response body to a file, using request, you could do something like this: 如果您只想使用请求将原始响应正文保存到文件中,则可以执行以下操作:

var formData = {
    filename: filename,
    file: fs.createReadStream(fileloc)
}
var req = request.post({url: 'http://myvendorsapi/Upload_PDF', formData: formData})
req.pipe(fs.createWriteStream('OUTPUT FILE NAME'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM