iview Upload组件多个文件上传

使用  iview Upload 上传组件 手动上传 包括单个文件和多个文件

思路:创建一个数组 把需要上传的文件 push到这个数组里面

1.引用组件

2.手动上传,根据官方文档 设置:before-upload ="handleUpload"等于false 

(1).:before-upload 是 iview Upload 上传组件的一个属性 设置返回值为 false 可以阻止默认上传方式(自动上传模式)

(2).handleUpload 是方法  *备注:代码在最后面

3.上传方法     

            //创建 formData 对象

                    let formData = new FormData();
                    //向 formData 对象中添加文件--这是其他参数
                    formData.append('jsid', _jsid);

                    //多个文件上传----------重点----需要吧已经存储到本地的文件加入 formData所以这里用for循环
for(var i=0; i< that.file.length; i++){ formData.append("uploadFile",that.file[i]); // 文件对象 }

HTML代码如下:

<FormItem label="应标资料" v-show="islook">
                    <template>
                        <Upload
                            multiple
                            ref="upload"
                            type="drag"
                            :format="['docx','doc','txt', 'pdf']"
                            :max-size="5000"
                            :before-upload="handleUpload"
                            :action="http">
                            <div style="padding: 20px 0">
                                <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                                <p>点击或者拖拽到此次上传文件</p>
                            </div>
                        </Upload>
                        <div>
                            <ul class="file-list" v-for="(list,index) in file" :key="index">
                                <li>文件名: <span style="font-size:15px;">{{ list.name }}</span> <Icon type="ios-close" size="20" style="float:right;" @click="delFileList(index)"></Icon></li>
                            </ul>
                        </div>
                    </template>
                </FormItem>


                <FormItem v-show="islookshenghe">
                    <h3>已经提交数据-正在等待审核</h3>
                    <Button type="primary" @click="gobackfanhui">返回</Button>
                </FormItem>

                <FormItem v-show="islook">
                    <Button type="primary" :loading="loading2" icon="ios-power" @click="upload">
                        <span v-if="!loading2">接受并提交应标信息</span>
                        <span v-else>正在上传文件中...</span>
                    </Button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                    <p style="color:red;font-size:15px;" v-show="isfiletihsi">请上传文件</p>
                </FormItem>

JS代码

delFileList(index){
                let that = this;
                that.file.splice(index, 1);
                
                console.log(that.file);
            }
handleUpload (file) {
                let that = this;
                if(that.file.length >= 5){
                    this.$Message.info("最多只能上传5个文件");
                }else{
                    that.file.push(file);
                }
                return false;
            }
axios提交方法代码:
upload(){
                let that = this;
                let _jsid = that.$route.query.id;
                if(that.file.length > 0){
                    that.loading2 = true;
                    //创建 formData 对象
                    let formData = new FormData();
                    //向 formData 对象中添加文件
                    formData.append('jsid', _jsid);

                    //多个文件上传
                    for(var i=0; i< that.file.length; i++){  
                      formData.append("uploadFile",that.file[i]);   // 文件对象    
                    } 

                    let config = {
                      headers: {
                        'Content-Type': 'multipart/form-data'
                      }
                    }

                    axios.post(that.http + "/shweb/gys/gysmsge/gysuploads.action", formData, {
                                timeout: 10000,
                                headers: {
                                    'Content-Type': 'multipart/form-data'
                                }
                            }).then(function (rdata) {
                                that.loading2 = false;
                                if(rdata.data == "0"){
                                    that.islook = false;
                                    that.islookshenghe = true;
                                }
                                console.log(rdata);
                            }).catch(function (error) {
                                that.loading2 = false;
                              that.$Message.error('服务器错误' + error);
                            });
                }else{
                    that.$Message.error("请至少上传一个文件");
                }
            }

猜你喜欢

转载自www.cnblogs.com/sjie/p/9456753.html