axios 请求后获取表单数据的文件名
getting filename of formdata after axios request
我有一个简短的问题。我想提取一个 wav 文件的文件名,我使用 formData 通过 axios 传递到我的后端。我不确定如何做到这一点是 nodejs。我将不胜感激
前端:
//file is a Blob
var new_file = new File([file], bucket_string);
var formData = new FormData();
formData.append("wavfile", new_file, bucket_string);
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
return await axios.post(`/files/upload-file`, formData, config);
//return await axios.post(`http://localhost:2000/files/upload-file`, data);
后端:
controller.js
async function uploadFile(req, res, next) {
fileService.uploadFile().then(function(val) {
res.json(val)
});
}
service.js
function uploadFile({ data }) {
const file = data;
var file_name = ?
//i would like to get the file name here
}
const express = require('express');
const multer = require('multer');
const app = express();
const upload = multer();
function uploadFile(req, res, next) {
console.log(req.file.originalname);
res.json({ok: true}).end();
}
app.post('/', upload.single('wavfile'), uploadFile);
app.listen(3001);
我有一个简短的问题。我想提取一个 wav 文件的文件名,我使用 formData 通过 axios 传递到我的后端。我不确定如何做到这一点是 nodejs。我将不胜感激
前端:
//file is a Blob
var new_file = new File([file], bucket_string);
var formData = new FormData();
formData.append("wavfile", new_file, bucket_string);
const config = {
headers: { 'content-type': 'multipart/form-data' }
}
return await axios.post(`/files/upload-file`, formData, config);
//return await axios.post(`http://localhost:2000/files/upload-file`, data);
后端:
controller.js
async function uploadFile(req, res, next) {
fileService.uploadFile().then(function(val) {
res.json(val)
});
}
service.js
function uploadFile({ data }) {
const file = data;
var file_name = ?
//i would like to get the file name here
}
const express = require('express');
const multer = require('multer');
const app = express();
const upload = multer();
function uploadFile(req, res, next) {
console.log(req.file.originalname);
res.json({ok: true}).end();
}
app.post('/', upload.single('wavfile'), uploadFile);
app.listen(3001);