I have a REST client on node, and I'm trying to upload pdf a file to another REST webserver which provides the ability to parse my pdf and extract some data. Basically it is a service. The npm package that I use is: https://www.npmjs.com/package/node-rest-client. If there are other rest clients, I can use those as well. The rest api I need to use is described below:
我在节点上有一个REST客户端,我正在尝试将pdf文件上传到另一个REST Web服务器,它提供了解析我的pdf并提取一些数据的能力。基本上它是一种服务。我使用的npm包是:https://www.npmjs.com/package/node-rest-client。如果还有其他休息客户,我也可以使用它们。我需要使用的其余api如下所述:
POST / ; Uploads a new PDF document via a form <br>
POST /file ; Uploads a new PDF document via bytestream
The question is how to upload the file. Also, I would like to see how to store the file at the other end.
问题是如何上传文件。另外,我想看看如何在另一端存储文件。
2 个解决方案
#1
11
You can use npm module request to upload the file. Here is a working example
您可以使用npm模块请求上传文件。这是一个有效的例子
var request = require('request');
var fs = require('fs');
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://yourdomain/file',
multipart: [
{
'content-type': 'application/pdf',
body: fs.createReadStream('image.png')
}
]
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
});
For receiving at the server side with node you can use modules like busboy. Here is a demo for this
要在服务器端接收节点,您可以使用busboy等模块。这是一个演示
var busboy = require('connect-busboy');
app.use(busboy());
app.use(function(req, res) {
if (req.busboy) {
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
// move your file etc
});
req.pipe(req.busboy);
}
});
#2
4
You can use request.
您可以使用请求。
There is an example for that
有一个例子
fs.createReadStream('file.pdf').pipe(request.post('http://example.com/file'))
#1
11
You can use npm module request to upload the file. Here is a working example
您可以使用npm模块请求上传文件。这是一个有效的例子
var request = require('request');
var fs = require('fs');
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://yourdomain/file',
multipart: [
{
'content-type': 'application/pdf',
body: fs.createReadStream('image.png')
}
]
},
function (error, response, body) {
if (error) {
return console.error('upload failed:', error);
}
console.log('Upload successful! Server responded with:', body);
});
For receiving at the server side with node you can use modules like busboy. Here is a demo for this
要在服务器端接收节点,您可以使用busboy等模块。这是一个演示
var busboy = require('connect-busboy');
app.use(busboy());
app.use(function(req, res) {
if (req.busboy) {
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
// move your file etc
});
req.pipe(req.busboy);
}
});
#2
4
You can use request.
您可以使用请求。
There is an example for that
有一个例子
fs.createReadStream('file.pdf').pipe(request.post('http://example.com/file'))