我正在尝试将文件发布到服务器。它返回200 OK响应,但程序无法读取文件内容。当我在文本文件中打开它们时,我看到边界,并且文件内容中存在一些标题。
File starts with ----------------------------802523244934076832438189
Content-Disposition: form-data; name="file"; filename="Test1.png"
Content-Type: image/png
and Ends with ----------------------------802523244934076832438189--
我的代码如下:
var formData = {
file:{
value: fs.createReadStream('./upload-folder/' + fileName),
options: {
filename: fileName,
contentType: req.body.attachment.mimeType //mimeType from JSON
}
}
};
var options = {
url: config.deployment.incidentUrl + '/attachment?filename=' + fileName,
method: "POST",
headers: { ContentType: "application/json"},
json: true,
formData: formData
};
request(options,
function (error, response, body) {
if (error) {
errorlog.error(`Error Message : PostAttachmentToCSMS : ${error}`);
}
else {
successlog.info(`Attachment posted for correlation Id: ${corIdFromJira}`);
}
});
答案 0 :(得分:0)
我认为这是因为使用formData
与使用json: true
是互斥的:数据 multipart/form-data
编码或 JSON - 编码,但不能两者兼得。 Content-Type
标题也不正确(虽然拼写错误但技术上不是问题)。
试试这个:
var options = {
url: config.deployment.incidentUrl + '/attachment?filename=' + fileName,
method: "POST",
formData: formData
};