【发布时间】:2019-03-23 04:11:10
【问题描述】:
函数的哪一部分在上传图片时提交我的表单?我不会在上传图片时提交表单。我想要提交按钮。代码的哪一部分使mi出现问题?有人认为此代码可以正常工作,但我想在上传照片自动化时提交表单。这段时间我的代码的哪一部分可能不需要我?
uploadFile(event) {
const formData = new FormData()
formData.append('image', event.target.files[0])
axios({
method: "post",
url: "linkmyapi",
data: formData,
headers: {
"Content-Type": "multipart/form-data"
}
})
.then(response => {
this.items.push(response.data);
this.image = "";
this.profile_image = ''
this.loading = false
this.dragAndDropUpload = false
this.styleObject.backgroundColor = ''
})
.catch(error => {
this.loading = false;
},
onDropFile(e) {
this.dragAndDropUpload = true
e.stopPropagation()
e.preventDefault()
let files = e.dataTransfer.files
this.createFile(files[0])
},
onChangeFile(e) {
// this.manualUpload = true
let files = e.target.files;
this.createFile(files[0])
},
createFile(file) {
if (!file.type.match('image.*')) {
alert('Select an image')
return
}
let reader = new FileReader()
let vm = this
reader.onload = function (e) {
vm.profile_image = e.target.result
}
reader.readAsDataURL(file)
this.uploadFile(event)
},
removeFile() {
this.profile_image = ''
this.styleObject.backgroundColor = ''
},
onDragOver () {
this.styleObject.backgroundColor = 'rgba(0, 160, 223, 0.4)'
},
onDragLeave () {
this.styleObject.backgroundColor = ''
},
HTML 是
<div class="upload-container">
<div
:style="styleObject"
class="drop drop-profile"
id="2"
@dragover.prevent="onDragOver()"
@dragleave.prevent="onDragLeave()"
@drop="onDropFile($event)"
:class="{ 'loading-image': loading }">
<label v-if="!profile_image" class="label-text label-text-profile">
Choose or drag
<br> and drop your
profile image
here
<br>
<input
type="file"
name="profile_image"
@change="onChangeFile($event)">
</label>
<div v-else class="hidden">
<img :src="profile_image" alt="Profile image" class="image-profile" />
<div v-if="!loading" class="lc-loupe-trash-container">
<div @click="removeFile" class="lc-trash"></div>
</div>
</div>
</div>
<div v-if="loading" class="spinner-container">
<i class="fa fa-spinner fa-spin"></i>
</div>
</div>
【问题讨论】:
-
你的 JavaScript 没有对应的 HTML 吗?
-
检查 devtools 网络选项卡,检查提交表单时是否调用了您的函数,提供更多代码以便我们了解您是如何监听该事件的。真的只是常识。
-
我无法检查它,因为我有 v-validate 并且如果未填写所有字段则无法提交表单
标签: javascript forms append