分片上传文件夹或者文件

1、在WebUploader的官网去下载文件包,因为项目基于jquery所以也要下载jquery文件,然后在放在public下新建的webUpload文件夹下。

2、在index.html的body后面引入jquer和插件js

3、vue3.0 在vue中进行封装

<template>
  <div class="upload"></div>
</template>

<script>
    export default {
        name: 'vue-upload',
        props: {
            accept: {
                type: Object,
                default: {},
            },
            // 上传地址
            url: {
                type: String,
                default: '',
            },
            // 上传文件名
            fileName: {
                type: String,
                default: '',
            },
            // 上传最大数量 默认为10
            fileNumLimit: {
                type: Number,
                default: 10,
            },
            // 大小限制 默认1M
            fileSingleSizeLimit: {
                type: Number,
                default:1024000,
            },
            // 上传时传给后端的参数,一般为token,key等
            formData: {
                type: Object,
                default: {}
            },
            // 生成formData中文件的key,下面只是个例子,具体哪种形式和后端商议
            keyGenerator: {
                type: Function,
                default(file) {
                    const currentTime = new Date().getTime();
                    const key = `${currentTime}.${file.name}`;
                    return key;
                },
            },
            multiple: {
                type: Boolean,
                default: false,
            },
            // 上传按钮ID
            uploadButton: {
                type: String,
                default: '',
            },
        },
        data() {
            return {
                uploader: null
            };
        },
        mounted() {
            this.initWebUpload();
        },
        methods: {
            initWebUpload() {
                this.uploader = WebUploader.create({
                    auto: true, // 选完文件后,是否自动上传
                    swf: '/public/webUploader/Uploader.swf',  // swf文件路径
                    server: this.url,  // 文件接收服务端
                    fileVal : this.fileName,
                    pick: {
                        id: this.uploadButton,     // 选择文件的按钮
                        multiple: this.multiple,   // 是否多文件上传 默认false
                        label: '',
                    },
                    accept: this.getAccept(this.accept),  // 允许选择文件格式。
                    threads: 3,
                    fileNumLimit: this.fileNumLimit, // 限制上传个数
                    fileSingleSizeLimit: this.fileSingleSizeLimit, // 限制单个上传图片的大小
                    formData: this.formData,  // 上传所需参数
                    chunked: true,          //分片上传
                    chunkSize: 1024000*this.fileSingleSizeLimit,    //分片大小
                    duplicate: true,  // 重复上传
                    compress:false,
                });
                this.uploader.on('beforeFileQueued',(file)=>{
                  this.$emit('beforeFile', file);
                })
                // 当有文件被添加进队列的时候,添加到页面预览
                this.uploader.on('fileQueued', (file) => {
                  this.$emit('fileChange', file);
                });

                this.uploader.on('uploadStart', (file) => {
                  // 在这里可以准备好formData的数据
                  this.uploader.options.formData.dirname = file.source.source.webkitRelativePath;
                });

                // 文件上传过程中创建进度条实时显示。
                this.uploader.on('uploadProgress', (file, percentage) => {
                  this.$emit('progress', file, percentage);
                });

                this.uploader.on('uploadSuccess', (file, response) => {
                  this.$emit('success', file, response);
                });

                this.uploader.on('uploadError', (file, reason) => {
                  this.$emit('uploadError', file, reason);
                });

                this.uploader.on('error', (type) => {
                    let errorMessage = '';
                    if (type === 'F_EXCEED_SIZE') {
                        errorMessage = `已阻止大小超过${this.fileSingleSizeLimit / (1024 * 1000)}M的文件`;
                    } else if (type === 'Q_EXCEED_NUM_LIMIT') {
                        errorMessage = `文件上传已达到最大上限数,已阻止超过${this.fileNumLimit}张的文件`;
                    } else {
                        errorMessage = `上传出错!请检查后重新上传!错误代码${type}`;
                    }
                    this.$emit('error', errorMessage);
                });

                this.uploader.on('uploadComplete', (file, response) => {
                    this.$emit('complete', file, response);
                });
            },

            upload(file) {
                this.uploader.upload(file);
            },
            stop(file) {
                this.uploader.stop(file);
            },
            // 取消并中断文件上传
            cancelFile(file) {
                this.uploader.cancelFile(file);
            },
            // 在队列中移除文件
            removeFile(file, bool) {
                this.uploader.removeFile(file, bool);
            },

            getAccept(accept) {
                switch (accept) {
                    case 'text':
                        return {
                            title: 'Texts',
                            exteensions: 'doc,docx,xls,xlsx,ppt,pptx,pdf,txt',
                            mimeTypes: '.doc,docx,.xls,.xlsx,.ppt,.pptx,.pdf,.txt'
                        };
                        break;
                    case 'video':
                        return {
                            title: 'Videos',
                            exteensions: 'mp4',
                            mimeTypes: '.mp4'
                        };
                        break;
                    case 'image':
                        return {
                            title: 'Images',
                            exteensions: 'gif,jpg,jpeg,bmp,png',
                            mimeTypes: '.gif,.jpg,.jpeg,.bmp,.png'
                        };
                        break;
                    default: return accept
                }
            },

        },
    };
</script>

<style lang="scss">
.webuploader-container {
	position: relative;
}
.webuploader-element-invisible {
	position: absolute !important;
	clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
    clip: rect(1px,1px,1px,1px);
}
.webuploader-pick {
	position: relative;
	display: inline-block;
	cursor: pointer;
	color: #00faff;
	text-align: center;
	overflow: hidden;
}
.webuploader-pick-hover {
}
.webuploader-pick-disable {
	opacity: 0.6;
	pointer-events:none;
}
</style>

4、在父组件中使用

//template 
<button id="filePicker"  >选择文件夹</button>
          <vue-upload
            ref="uploader"
            :accept='accept'
            :fileName='fileName'
            :formData='formData'
            :url="uploadUrl"
            uploadButton="#filePicker"
            multiple
            @success='onFileSuccess'
            @uploadError='uploadError'
            @error="onError"
          ></vue-upload>

//data
uploadUrl:base.baseURL+'/api/pic/upload',
formData:{},
accept:{  //接收文件类型
    title: 'Images',
    extensions: 'gif,jpg,jpeg,bmp,png',
    mimeTypes: 'image/*'
},
发布了25 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zerogf/article/details/102785100