如果你不在 vue Js 上上传照片,你如何处理它?



如果你不在vue Js上上传照片,你该如何处理??.. 我有一个文章编辑案例,当我编辑一篇文章时,通常只编辑文章的内容,否则就只是文章标题的名称,很少更新照片…例如,如果照片未更新/为空,我该如何处理?我不希望如果照片不上传,那么旧照片就不会改变。

如果为空错误"TypeError: Cannot read property 'name' of undefined"

..这是我的代码

let formData = new FormData();
if (this.$refs.file.files[0] != null){
formData.append(
"img",
this.$refs.file.files[0],
this.$refs.file.files[0].name
);
} else {
// this here in what content ?
}

而不是使用this.$ref。尝试使formData property in data function循环遍历键值对象

//this is my example straight from my project
data() {
return {
formData: {
id: null,
patient_id: this.patient.id,
admission_date: "",
discharge_date: "",
provider: "",
hospital: "",
reason: "",
attachment: null,
},
};
},
methods: {
submitFormData() {
let form = new FormData();
// here when data is submitted formData key value is being looped through new FormData
for (var key in this.formData) {
form.append(key, this.formData[key]);
}
axios
.post("api.example.com/store", form, {
header: {
"Content-Type":
"multipart/form-data"
},
})
.then((res) => {
console.log(res)
})
.catch(err => console.log(err.message));
},

文件附件在本例中最初是attachment:null

最新更新