在对 Vue2 Dropzone 进行了一些研究之后
手动添加文件
使用该manuallyAddFile
方法允许您以编程方式将文件添加到放置区。例如,如果您的服务器上已经有文件要预先填充您的 dropzone 区域,则只需在触发 vdropzone-mounted 事件时使用该函数。
资源
因此,解决方案是检查队列中是否需要处理任何内容。由于您手动添加了已有的文件,因此无需再次上传。只有当用户添加新文件时才需要上传。
你可以通过几种方式做到这一点,但我会推荐类似下面的例子,因为它很简单:
onSubmit() {
if (this.$refs.myVueDropzone.getQueuedFiles().length) {
this.$refs.myVueDropzone.processQueue()
}
}
如果您需要等到队列完成处理才能发送您的表单,您可以利用 vdropzone-queue-complete。下面是一个简单的例子:
<template>
<!-- SOME FORM ELEMENTS HERE -->
<vue-dropzone
ref="myVueDropzone"
:options="dropzoneOptions"
@vdropzone-error="errorUploading"
@vdropzone-success="fileUploaded"
@vdropzone-queue-complete="submitForm"
/>
<button @click="saveForm">Save</button>
</template>
<script>
export default {
methods: {
saveForm () {
if (this.$refs.myVueDropzone.getQueuedFiles().length) {
this.$refs.myVueDropzone.processQueue()
} else {
this.submitForm()
}
},
errorUploading (file, message, xhr) {
// do something when a file errors
// perhaps show a user a message and create a data element to stop form from processing below?
},
fileUploaded (file, response) {
// do something with a successful file upload. response is the server response
// perhaps add it to your form data?
},
submitForm () {
// Queue is done processing (or nothing to process), you can now submit your form
// maybe check for errors before doing the below step
// do what ever you need to submit your form this.$axios.post(...) etc.
}
}
}
</script>