我正在尝试使用以下命令将图像上传到服务器,并同时传递一些附加数据(在同一发布请求中): VueJS 2(CLI 3),axios,multer,sharp,并且我有NodeJS与后端是MongoDB。
前端:
<form @submit.prevent="onSubmit" enctype="multipart/form-data">
<div class="input">
<label for="name">Name: </label>
<input
type="text"
id="name"
v-model="name">
</div>
<div class="input">
<label for="last_name">Your last_name: </label>
<input
type="text"
id="last_name"
v-model="last_name">
</div>
<div class="input">
<label for="permalink">permalink</label>
<input
type="text"
id="permalink"
v-model="permalink">
</div>
<div class="input">
<label for="price">price</label>
<input
type="text"
id="price"
v-model="price">
</div>
<div class="input">
<label for="photo">photo</label>
<input
style="display: none"
type="file"
id="photo"
@change="onFileSelected"
ref="fileInput">
<div @click="$refs.fileInput.click()">Pick file</div>
</div>
<div class="submit">
<md-button type="submit" class="md-primary md-raised">Submit</md-button>
</div>
</form>
VueJS方法:
import axios from 'axios'
export default {
data () {
return {
name: '',
last_name: '',
permalink: '',
selectedFile: null,
url: null,
price: 0,
second: false
}
},
methods: {
onFileSelected (event) {
this.selectedFile = event.target.files[0]
this.url = URL.createObjectURL(this.selectedFile)
},
onUpload () {
const fd = new FormData()
fd.append('image', this.selectedFile, this.selectedFile.name)
axios.post('http...', fd, {
onUploadProgress: uploadEvent => {
console.log('Upload Progress ' + Math.round(uploadEvent.loaded / uploadEvent.total * 100) + ' %')
}
})
.then(res => {
console.log(res)
})
},
onSubmit () {
const fd = new FormData()
fd.append('image', this.selectedFile, this.selectedFile.name)
fd.append('data', this.name, this.last_name)
axios.post('http://localhost:7000/api/create-user', fd, {
onUploadProgress: uploadEvent => {
console.log('Upload Progress ' + Math.round(uploadEvent.loaded / uploadEvent.total * 100) + ' %')
}
})
.then(res => {
console.log(res)
if (res.data === 'ok') {
this.second = true
}
})
.then(
setTimeout(function () {
this.second = false
this.reset()
}.bind(this), 2000)
)
.catch(error => console.log(error))
}
}
}
NodeJS:
controller.postCreateUser = (req, res) => {
const sharp = require('sharp');
const fs = require('fs');
const folderImg = 'backend/uploads/';
console.log(JSON.stringify(req.body));
console.log(req.file);
res.send("ok");
};
req.file的结果是(很好):
{ fieldname: 'image',
originalname: 'musk.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: 'backend/uploads/original/',
filename: 'musk-1545470459038.jpg',
path: 'backend\\uploads\\original\\musk-1545470459038.jpg',
size: 108787 }
console.log(req.body)的结果是
{"data":""}
问题,这里的数据为空字符串,我没有收到任何数据。我需要将数据存储到数据库中。该怎么做?
如果您不太清楚,请询问我更多详细信息。
答案 0 :(得分:1)
在您的onSubmit
方法中,您可以执行以下操作:
const fd = new FormData()
fd.append('image', this.selectedFile, this.selectedFile.name)
fd.append('data', this.name, this.last_name)
但是FormData.append()需要以下参数:
name
-其数据包含在value中的字段的名称。value
-字段的值。这可以是USVString或Blob(包括子类,例如File)。filename
可选-在将Blob或文件作为第二个参数传递时,报告给服务器的文件名(USVString)。 第三个参数不适用于此行:fd.append('data', this.name, this.last_name)
相反,您可以执行以下任一操作:
fd.append('data', `${this.name} ${this.last_name}`) // Send a single String
或
// Send both Strings separately
fd.append('name', this.name)
fd.append('last_name', this.last_name)
或
// Send the data as JSON
fd.append('data', JSON.stringify({name: this.name, last_name: this.last_name}))