我在穆特中使用nuxt3 3/node.js,无法在服务器中存储或接收文件,也无法在根中看到req.file,它始终是空的。服务器代码:
const upload = multer({ dest: './uploads/' })
router.post('/sendNewQuestionCSV', upload.single('csv'),adminControler.sendNewQuestionCSV.bind(adminControler))我的FrontEnd代码和nuxt3:
async function fileSelected(e) {
let formData = new FormData();
formData.append("csv", e.target.files[0]);
const res = await $postFetch("/admin/sendNewQuestionCSV", formData, {
"Content-Type": "multipart/form-data",
});
}注意:$postFetch是我自己使用的fetch方法,第三个参数是"headers“。它是一个nuxt3插件
这个插件代码:
export default defineNuxtPlugin(async () => {
return {
provide: {
postFetch: async (url, body,headers={}) => {
let token = useCookie('token');
return await $fetch(url, {
method: 'post',
body: { token: token.value, ...body },
baseURL: useRuntimeConfig().API_BASE,
...headers
}).catch((error) => error.data)
}
}
}
})发布于 2022-06-03 16:48:09
尝试使用.append添加token
postFetch: async (url, body,headers={}) => {
let token = useCookie('token');
body.append('token', token.value);
return await $fetch(url, {
method: 'post',
body: body,
baseURL: useRuntimeConfig().API_BASE
}).catch((error) => error.data)
}编辑
还可以尝试删除headers
https://stackoverflow.com/questions/72492442
复制相似问题