![](/img/trans.png)
[英]NodeJS not trigger save file dialog in browser / EmberJS not receive file sent from server
[英]How to save a file sent from NodeJS with POST
我創建了以下函數來加載流文件。 該流被發送到用 POST 指定的地址。 在目的地,我有另一個 NodeJS 服務,它使用 ExpressJS“捕獲”此 POST,但我不知道如何將文件保存到目的地。
我電腦上的 NodeJS:
function sendFileToRaspberry(filePath) {
var options = {
method: 'POST',
url: 'http://x.x.x.x:8080/api/print',
qs: {file: fs.createReadStream(filePath)},
headers:
{
'cache-control': 'no-cache'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
}
我的樹莓派上的 NodeJS:
app.post('/api/print', function (req, res) {
ORIGINAL_IP = req.ip.replace("::ffff:", '');
console.log("Received a print request from: " + ORIGINAL_IP);
// Test history data for /api/printer API
var file = req.query.file;
console.log("Test data: " + file);
util.inspect(file);
});
但在控制台日志中我得到:
收到來自:yyyy 的打印請求
測試數據:[object Object]
如何在 Rasp 上保存此文件?
您可以使用 expressjs 中間件助手,如express-fileupload
、 multer 。
我認為,您已經使用Multipart Form Uploads
通過 http POST 上傳文件。
客戶端(您的 PC)
function sendFileToRaspberry(filePath) {
var options = {
method: 'POST',
url: 'http://x.x.x.x:8080/api/print',
formData: {
file: fs.createReadStream(filePath)
},
headers: {
'cache-control': 'no-cache'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.