POST 文件使用请求、gridfs-stream 和 multer
POST file using request, gridfs-stream and multer
在我的 node.js 应用程序中,我需要使用 GridFS 从 MongoDB 读取数据并通过 POST
请求将其上传到另一台服务器,我正在使用模块 gridfs-stream
、request
和 multer
.
根据 the request
docs,我可以简单地为 multipart/form-data
添加一个流,所以我这样做:
var fileUuid = "myFilename";
var options = {
url: "url.to/my/target/",
method: "POST",
header: {"Content-Type": "multipart/form-data"},
formData: {
filedata: gfs.createReadStream({filename: fileUuid})
}
}
request(options, function(error, response, body) {
if (error) {
console.log("[" + getDateTime() + "] Error replicating file:");
console.log(error);
return;
}
console.log('statusCode: ', response && response.statusCode);
console.log('body: ', body);
});
在我的接收服务器上,我有以下代码:
var upload = multer({ dest: './tmp_upload/' })
app.post('/my/target', upload.single('filedata'), function(req, res) {
console.log('['+getDateTime()+'] Request received');
}
但是每当我执行请求时,我在发送服务器上收到以下错误:
[2019:07:26:16:52:00] Error replicating file:
Error: write ECONNRESET
在我的接收服务器上:
Error: Unexpected end of multipart data
at c:\Users\...\node_modules\dicer\lib\Dicer.js:62:28
at process._tickCallback (internal/process/next_tick.js:61:11)
任何人都可以给我提示如何POST
我的文件从一台服务器到另一台服务器吗?
编辑:
问题似乎是 gfs.createReadStream({filename: fileUuid})
类型的流...当我先将文件写入文件系统并将 fs.createReadStream(...)
作为 formData 放入表单中时,它就可以工作了。
但是是否可以选择将流直接导入 formData,而不必先将其写入磁盘?
所以我最终使用了一个从数据库中读取的临时文件,将其写入磁盘并创建一个新的 readStream
,然后将其放入请求中。请求成功后,我从我的磁盘中删除临时文件并继续我的代码:
var fsstreamwrite = fs.createWriteStream("./tmp/" + myFile);
var readstream = gfs.createReadStream( {filename: myFile} );
readstream.pipe(fsstreamwrite);
readstream.on("close", function () {
console.log("File read successfully from database");
var options = {
url: "url.to/my/target/",
method: "POST",
enctype: "multipart/form-data",
formData: {
"filedata": fs.createReadStream('./tmp/' + myFile)
}
}
request(options, function(error, response, body) {
if (error) {
// error handling
return;
}
// delete temporary file
fs.unlinkSync('./tmp/' + myFile)
// continue with my stuff...
});
});
在我的 node.js 应用程序中,我需要使用 GridFS 从 MongoDB 读取数据并通过 POST
请求将其上传到另一台服务器,我正在使用模块 gridfs-stream
、request
和 multer
.
根据 the request
docs,我可以简单地为 multipart/form-data
添加一个流,所以我这样做:
var fileUuid = "myFilename";
var options = {
url: "url.to/my/target/",
method: "POST",
header: {"Content-Type": "multipart/form-data"},
formData: {
filedata: gfs.createReadStream({filename: fileUuid})
}
}
request(options, function(error, response, body) {
if (error) {
console.log("[" + getDateTime() + "] Error replicating file:");
console.log(error);
return;
}
console.log('statusCode: ', response && response.statusCode);
console.log('body: ', body);
});
在我的接收服务器上,我有以下代码:
var upload = multer({ dest: './tmp_upload/' })
app.post('/my/target', upload.single('filedata'), function(req, res) {
console.log('['+getDateTime()+'] Request received');
}
但是每当我执行请求时,我在发送服务器上收到以下错误:
[2019:07:26:16:52:00] Error replicating file:
Error: write ECONNRESET
在我的接收服务器上:
Error: Unexpected end of multipart data
at c:\Users\...\node_modules\dicer\lib\Dicer.js:62:28
at process._tickCallback (internal/process/next_tick.js:61:11)
任何人都可以给我提示如何POST
我的文件从一台服务器到另一台服务器吗?
编辑:
问题似乎是 gfs.createReadStream({filename: fileUuid})
类型的流...当我先将文件写入文件系统并将 fs.createReadStream(...)
作为 formData 放入表单中时,它就可以工作了。
但是是否可以选择将流直接导入 formData,而不必先将其写入磁盘?
所以我最终使用了一个从数据库中读取的临时文件,将其写入磁盘并创建一个新的 readStream
,然后将其放入请求中。请求成功后,我从我的磁盘中删除临时文件并继续我的代码:
var fsstreamwrite = fs.createWriteStream("./tmp/" + myFile);
var readstream = gfs.createReadStream( {filename: myFile} );
readstream.pipe(fsstreamwrite);
readstream.on("close", function () {
console.log("File read successfully from database");
var options = {
url: "url.to/my/target/",
method: "POST",
enctype: "multipart/form-data",
formData: {
"filedata": fs.createReadStream('./tmp/' + myFile)
}
}
request(options, function(error, response, body) {
if (error) {
// error handling
return;
}
// delete temporary file
fs.unlinkSync('./tmp/' + myFile)
// continue with my stuff...
});
});