Reprinted: https://blog.csdn.net/sunday_tutu/article/details/82018037
The function I want to achieve is to check whether there is duplicate data in the table before uploading the file. If there is, a pop-up window prompts whether to overwrite it. After confirming, the upload will continue. After canceling, the upload will not be uploaded again.
The element-ui used in the project is V1.4.3
-
<el-upload
-
class="upload-demo" drag
-
ref="fileUpload"
-
:action="urls.fileUpload"
-
:on-success="handleUploadSuccess"
-
:on-error="handleUploadError"
-
:on-progress="progressUpload"
-
:before-upload="beforeUpload"
-
show-file-list
-
multiple>
-
<i class="el-icon-upload"></i>
-
<div class="el-upload__text">点击上传,或者拖拽到这里</div>
-
</el-upload>
In the code, I return the before-upload method to false, and then after clicking confirm, call _this.$refs.fileUpload.submit(); but after clicking OK, the file is still not uploaded. There is a second problem, which is to cancel When _this.$refs.fileUpload.clearFiles(); I called the clearFiles() method, this method will empty the file list, I only want to delete the file I cancelled at that time.
I went to look at the element-ui source code later and found that if the before-upload method returns false, the submit() method will be intercepted. The source code is as follows:
In the source code, the post method in the child component is finally called. If you don’t know which child component you can print out _this.$refs.fileUpload.$children
So I replaced these two sentences with the following two sentences:
Continue uploading: _this.$refs.fileUpload.$children[0].post(file);
delete the file in the file list when canceling: _this.$refs.fileUpload.handleRemove(file);
const file = this.$refs.enterUpload.fileList[0].raw
this.$refs.enterUpload.$children[0].post(file);