简体   繁体   English

使用nodejs / npm的请求包进行发布时,如何发布普通文件缓冲区而不是二进制编码文件?

[英]How do I post a plain file buffer instead of binary-encoded-file when posting with nodejs/npm's request package?

I am using npm's request package to do a post of a file-buffer to a REST api written using meteor.js restivus package. 我正在使用npm的request包对使用meteor.js restivus包编写的REST api进行文件缓冲区发布。 My node.js client code that posts to the api is as follows: 我发布到api的node.js客户端代码如下:

url = 'http://localhost:3000/api/v1/images/';

fs.readFile('./Statement.odt', function read(err, data) { if (err) { throw err; } console.log(data); //At this stage the file is still a buffer - which is correct var file = data; request.post({ uri: url, headers: { 'X-User-Id': userId, 'X-Auth-Token': authToken }, form: { file: file, //Inside the request.post the file is converted to binary encoding name:"Statement.odt" } }, function(err, httpResponse, body) { if (err) { return console.error('post failed:', err); } console.log('Get successful! Server responded with:', body); }); });

The problem/issue here is that inside the request.post the file is converted to binary-encoded blob. 问题/问题是在request.post内部将文件转换为二进制编码的Blob。 See my comments in the "form:" property of the first argument of "request.post" in the code above. 请参阅上面代码中“ request.post”的第一个参数的“ form:”属性中的注释。 This becomes a problem on my meteor.js server where the file is required as a buffer instead of a binary encoded file. 这在我的meteor.js服务器上成为一个问题,在该服务器上,文件需要作为缓冲区而不是二进制编码文件。 (For info: I am using Ostr-io/files' GridFS to store the file - it requires the file to be a buffer) (有关信息:我使用的是Ostr-io / files的GridFS来存储文件-它要求文件成为缓冲区)

If there is no way other than to pass the file as an encoded string, then is there a way to convert that encoded blob back to a buffer server-side where I am using/talking meteor.js? 如果除了将文件作为编码字符串传递之外别无选择,那么是否有办法将编码的blob转换回我正在使用/通话meteor.js的缓冲服务器端? Please help! 请帮忙!

If you need more info, please let me know, I'll provide. 如果您需要更多信息,请告诉我,我会提供。

I found the answer to my own question. 我找到了自己问题的答案。 My node.js client code that posts to the api changes as follows: 我发布到api的node.js客户端代码更改如下:

  url = 'http://localhost:3000/api/v1/images/'; fs.readFile('./Statement.odt', function read(err, data) { if (err) { throw err; } console.log(data); //var file = new Buffer(data).toString('base64'); var file = data; //var file = fs.readFileSync('./Statement.odt',{ encoding: 'base64' }); var req = request.post({ uri: url, headers: { 'X-User-Id': userId, 'X-Auth-Token': authToken }, /*form: { //Post to the body instead of the form file: file, name:"Statement.odt", },*/ body: file, //Post to the body instead of the form json:true //Set json to true //encoding: null }, function(err, httpResponse, body) { if (err) { return console.error('post failed:', err); } console.log('Get successful! Server responded with:', body); }); }); 

On the server-side access the json data as follows and convert the file back to a buffer: 在服务器端,按以下方式访问json数据,然后将文件转换回缓冲区:

 var bufferOriginal = Buffer.from(this.request.body.data) 

Note that when you do console.log(bufferOriginal) you get an output of the file encoded in utf8 but when you reference/use bufferOriginal anywhere in the code it is recognized as a file buffer that looks somenhing like: 请注意,当您执行console.log(bufferOriginal)时,您会获得以utf8编码的文件的输出,但是当您在代码中的任何地方引用/使用bufferOriginal时,该文件将被识别为类似于以下内容的文件缓冲区:

 <Buffer 50 4b 03 04 14 00 00 08 00 00 00 55 f6 4c 5e c6 32 0c 27 00 00 00 27 00 00 00 08 00 00 00 6d 69 6d 65 74 79 70 65 61 70 70 6c 69 63 61 74 69 6f 6e 2f ... > 

Thanks to @Dr.Dimitru for nudging me in the direction towards the solution and for pointing out that this.request.body is already json 感谢@ Dr.Dimitru向我介绍解决方案的方向,并指出this.request.body已经是json了

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

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