节点JS将二进制文件上传到另一台服务器 [英] Node JS upload binary file to another server
问题描述
我有一个音频文件,我将其发布到服务器上进行翻译.我已经设法在邮递员中创建了一个请求,但是我不知道如何将文件写入该服务器.以下是到目前为止的代码:
I've got an audio file which I post to a server for translation. I've managed to create a request in postman, but I do not know how to write the file to this server. Below is the code I have got so far:
var http = require("https");
var options = {}
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
options{}
填充了方法/主机名/课程端口.在邮递员中,我添加了二进制"文件,但是我无法弄清楚如何将文件写入Node JS中的请求.
options{}
is filled with method/hostname/port ofcourse. In postman I add "binary" file, but I cannot figger out how to write a file to the request in Node JS.
推荐答案
One solution that would easily fit into your current program without too much work is to make use of the form-data module on npm.
表单数据模块简化了节点中的多部分请求.以下是使用方法的简单示例.
The form-data module makes ease of multipart requests in node. The following is a simple example of how to use.
var http = require("https");
var FormData = require('form-data');
var fs = require('fs')
var form = new FormData();
form.append('my_field', fs.createReadStream('my_audio.file'));
var options = {
host: 'your.host',
port: 443,
method: 'POST',
// IMPORTANT!
headers: form.getHeaders()
}
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
// Pipe form to request
form.pipe(req);
在现实世界"场景中,您将需要做更多的错误检查.另外,npm上还有许多其他http客户端,也使此过程变得容易(请求模块使用表格数据BTW).检出请求和
In a "real-world" scenario you would want to do a lot more error checking. Also, there are plenty of other http clients on npm that make this process easy as well (the request module uses form-data BTW). Check out request, and got if you are interested.
对于发送二进制请求,基本原理仍然相同,req
是可写流.这样,您可以将pipe
数据放入流中,或直接使用req.write(data)
进行写入.这是一个例子.
For sending a binary request the fundamentals are still the same, req
is a writable stream. As such, you can pipe
data into the stream, or write directly with req.write(data)
. Here's an example.
var http = require('https');
var fs = require('fs');
var options = {
// ...
headers: {
'Content-Type': 'application/octet-stream'
}
}
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
var audioFile = fs.createReadStream('my_audio.file', { encoding: 'binary' });
audioFile.pipe(req);
请注意,如果您明确地使用write
方法req.write(data)
,则必须调用req.end()
.另外,您可能想看看Node Buffer
的编码选项(文档).
Note, that if you use the write
method explicitly req.write(data)
you must call req.end()
. Also, the you may want to take a look at the encoding options for Node's Buffer
(docs).
这篇关于节点JS将二进制文件上传到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!