How to upload files sent with FormData through Axios ? (TypeError: file.mv is not a function)
How to upload files sent with FormData through Axios ? (TypeError: file.mv is not a function)
所以我使用 Axios 从一台服务器向另一台服务器发送文件,一个是应用程序后端,另一个是区块链服务器。
我将文件发送到哪里:
router.post("/acme/:id", auth, async (req, res) => {
var formData = new FormData();
console.log(req.files.file)
formData.append("image", req.files.file.data);
var Response;
try {
Response = await axios.post(BC_SERVER + "acmeDataFileUpload", {
id: req.params.id,
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
});
} catch (error) {
console.log("Error BlockChain");
}
try {
res.status(201).send("ok");
} catch (e) {
res.status(500).send(e);
}
});
Axios 将它发送到哪里:
app.post('/acmeDataFileUpload', async (req, res) => {
const id_owner = req.body.id;
console.log(req.body)
const file = req.body.data;
const fileName = id_owner;
const filePath = 'files/' + fileName;
console.log(fileName);
file.mv(filePath, async (err) => {
try {
const fileHash = await addFile(fileName, filePath);
fs.unlink(filePath, (err) => {
if (err) console.log(err);
});
const json = '{"dataType": "Object" , "local": "'+localServer+fileHash+'",' +'"ipfsServer": "'+ipfsServer+fileHash+'"}';
console.log(json);
const obj = JSON.parse(json);
res.status(201).send(obj);
} catch (err) {
console.log('Error : failed to download file');
console.log(err);
return res.status(500).send(err);
}
});
});
这就是 req.body
的日志:
{
id: '5ec2b7d47ae93a49ecb773f6',
data: {
_overheadLength: 144,
_valueLength: 579564,
_valuesToMeasure: [],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: false,
_streams: [
'----------------------------383350625990492694059785\r\n' +
'Content-Disposition: form-data; name="image"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n',
[Object],
null
],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------383350625990492694059785'
},
headers: { 'Content-Type': 'multipart/form-data' }
}
基本上我在这里发送缓冲区,因为 FormData 不接受文件并告诉我 source.on 不是函数 我宁愿发送我的图像req.files
而不是 req.body
,Axios 真的让我很困惑。
headers: formData.getHeaders()
所以我使用 Axios 从一台服务器向另一台服务器发送文件,一个是应用程序后端,另一个是区块链服务器。
我将文件发送到哪里:
router.post("/acme/:id", auth, async (req, res) => {
var formData = new FormData();
console.log(req.files.file)
formData.append("image", req.files.file.data);
var Response;
try {
Response = await axios.post(BC_SERVER + "acmeDataFileUpload", {
id: req.params.id,
data: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
});
} catch (error) {
console.log("Error BlockChain");
}
try {
res.status(201).send("ok");
} catch (e) {
res.status(500).send(e);
}
});
Axios 将它发送到哪里:
app.post('/acmeDataFileUpload', async (req, res) => {
const id_owner = req.body.id;
console.log(req.body)
const file = req.body.data;
const fileName = id_owner;
const filePath = 'files/' + fileName;
console.log(fileName);
file.mv(filePath, async (err) => {
try {
const fileHash = await addFile(fileName, filePath);
fs.unlink(filePath, (err) => {
if (err) console.log(err);
});
const json = '{"dataType": "Object" , "local": "'+localServer+fileHash+'",' +'"ipfsServer": "'+ipfsServer+fileHash+'"}';
console.log(json);
const obj = JSON.parse(json);
res.status(201).send(obj);
} catch (err) {
console.log('Error : failed to download file');
console.log(err);
return res.status(500).send(err);
}
});
});
这就是 req.body
的日志:
{
id: '5ec2b7d47ae93a49ecb773f6',
data: {
_overheadLength: 144,
_valueLength: 579564,
_valuesToMeasure: [],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: false,
_streams: [
'----------------------------383350625990492694059785\r\n' +
'Content-Disposition: form-data; name="image"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n',
[Object],
null
],
_currentStream: null,
_insideLoop: false,
_pendingNext: false,
_boundary: '--------------------------383350625990492694059785'
},
headers: { 'Content-Type': 'multipart/form-data' }
}
基本上我在这里发送缓冲区,因为 FormData 不接受文件并告诉我 source.on 不是函数 我宁愿发送我的图像req.files
而不是 req.body
,Axios 真的让我很困惑。
headers: formData.getHeaders()