服务器 "undefined" 中的上传问题,因为我想要文件名
Upload problem in server "undefined" because I wanted the name of the file
大家好,我只是想知道在我的对象中哪里可以找到文件名。我做了一个 console.log 来找到 属性 但我有点迷路了。我试着做 ${data.name} 但它说未定义。有人知道我在请求中的什么地方可以找到它吗?
//the function
async onUpload() {
const formData = new FormData();
formData.append('image', this.ModalFile, this.ModalFile.name);
await axios.post('/publications/uploads', formData, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${this.$store.state.token}`,
},
//router
router.post('/uploads', upload.single('image'), async (request, response) => {
const data = await request.files[0].buffer;
console.log(data);
await fs.writeFile(`./uploads/${data.name}`, data);
return response.status(200).json({ uploaded: true });
}); ```
我认为 属性 已开启:request.files[0]
所以,应该是:request.files[0].name
尝试在等待缓冲区
之前使用console.log( request.files[0] )
file = request.FILES[0]
file.name # Gives name
file.content_type # Gives Content type text/html etc
file.size # Gives file's size in byte
file.buffer # Gives file's content as a buffer
file.read() # Reads file
大家好,我只是想知道在我的对象中哪里可以找到文件名。我做了一个 console.log 来找到 属性 但我有点迷路了。我试着做 ${data.name} 但它说未定义。有人知道我在请求中的什么地方可以找到它吗?
//the function
async onUpload() {
const formData = new FormData();
formData.append('image', this.ModalFile, this.ModalFile.name);
await axios.post('/publications/uploads', formData, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${this.$store.state.token}`,
},
//router
router.post('/uploads', upload.single('image'), async (request, response) => {
const data = await request.files[0].buffer;
console.log(data);
await fs.writeFile(`./uploads/${data.name}`, data);
return response.status(200).json({ uploaded: true });
}); ```
我认为 属性 已开启:request.files[0]
所以,应该是:request.files[0].name
尝试在等待缓冲区
之前使用console.log( request.files[0] )
file = request.FILES[0]
file.name # Gives name
file.content_type # Gives Content type text/html etc
file.size # Gives file's size in byte
file.buffer # Gives file's content as a buffer
file.read() # Reads file