![](/img/trans.png)
[英]Upload file in node.js and POST multipart in spring boot
[英]Node.js POST request with multipart file as parameter
我需要在Node.js中向具有兩個參數的API發出POST請求:1)“元數據”,一個字符串,以及2)“ file”,一個多部分文件。
以下是服務器端Java代碼的一部分:
public ResponseEntity<CmisDocumentDTO> createDocument(
@ApiParam(name = "file", value = "File to be uploaded.", required = true) @RequestParam("file") MultipartFile file,
@ApiParam(name = "metadata", value = "Metadata of the document", required = false) @RequestParam("metadata") String metadata) {
//Calls the service
}
以下是我的node.js代碼調用此請求。 該文件在我的本地計算機上,並使用表單數據模塊:
var FormData = require('form-data');
var form = new FormData();
form.append("metadata", "metadata_string_goes_here");
form.append("file", fs.createReadStream(fileName));
var request = https.request({
method: 'post',
host: 'example.org',
path: '/upload',
"rejectUnauthorized": false,
headers: form.getHeaders()
});
form.pipe(request);
request.on('response', function(res) {
console.log(res.statusCode);
});
當我運行此代碼時,將返回內部服務錯誤(代碼:500),該錯誤指示MultipartFile參數“ file”不存在。
如何成功提交包含多部分文件作為參數的POST請求?
謝謝!
fileName應該是本地計算機中文件的路徑
fs.createReadStream(fileName)
如果您沒有給出以root開頭的絕對路徑,則相對路徑可以工作,但是它們相對於process.cwd()而不是當前正在執行的模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.