Chicken Soup of the Day: Pessimists Might Be Right, But Optimists Often Succeed
Suppose there is a requirement that the uploaded pdf document should not be larger than 10M
Using the vue-simple-uploader plugin, we need to verify in the fileAdded event. After version 1.0.0, if you want to stop uploading, then return false, continue uploading return true
vue-simple-uploader - npm A Vue.js upload component powered by simple-uploader.js. Latest version: 0.7.6, last published: 3 years ago. Start using vue-simple-uploader in your project by running `npm i vue-simple-uploader`. There are 81 other projects in the npm registry using vue-simple-uploader. https://www.npmjs.com/package/vue-simple-uploader/v/1.0.1 However, there is a Pit, now [July 25, 2023] the fileAdded method does not support asynchronous, that is to say, the following code will not prevent file upload
// 这是一个有问题的方法,return false 并不会阻止文件上传
const fileAdded = async (rootFile: any) => {
const size = rootFile.size
// 理想的是,等待 checkFun 方法返回,再决定是否继续上传
const canUpload = await checkFun(size)
if (!canUpload) {
return false
}
return true
}
The solution is
- Set autoStart = "false" [default is true]
- Call this.$refs.uploaderRef.uploader.upload() method to upload manually
<uploader
ref="uploaderRef"
class="uploader"
:options="options"
:file-status-text="statusText"
@fileAdded="fileAdded"
:autoStart="false"
@fileSuccess="fileSuccess"
@uploadStart="uploadStart"
@fileError="fileError"
>
<!-- 其他内容 -->
</uploader>
// 这个方法没有问题
const fileAdded = async (rootFile: any) => {
const size = rootFile.size
const canUpload = await checkFun(size)
if (canUpload) {
// 校验通过,手动调用上传方法
this.$refs.uploaderRef.uploader.upload()
}
}