iviewui Upload上传爬坑,手动上传,二次上传

##Upload 实现二次上传

因为不熟悉iview 组件,在做上传的时候,查看公司之前项目都是直接上传,但是到我手里的项目是需要先走一下
接口进行上传文件验证 /(ㄒoㄒ)/~~ ,然后增加其他参数再上传 

直接上例子

描述: 需要先走验证,然后手动上传

额外参数

额外参数增加好说,在项目文档中有说明 增加data变量,将增加的参数都放到 data 中 额外参数就解决了
 <Upload 
        :action="actionUrl"
        ref="upLoadRef"
        :show-upload-list="false" 
        :on-success="uploadFile" 
        :before-upload="beforUp"
        :data="upOtherData"
        :headers="uploadHeader">
        <Button class="otherBtn"><i style="padding-right:4px" class="icon-scdr"></i>上传         </Button>
        </Upload>
参数说明:、
  action:上传的请求接口  我使用的变量 也就是校验的请求,之后确定上传修改这个变量
  show-upload-list: 是否显示上传之后的列表 
  on-success:上传成功之后 (response, file, fileList)参数,我这里拿到返回之后文件名称
  before-upload:上传之前(res) 可以拿到要上传的文件 res后面有用
  data: 确定上传增加额外参数
  headers:可以给请求头增加参数 后端要求token,所以里面放了token

以上就是我的设置,一开始方向错误,我一直在纠结参数格式,用组件上传会默认将参数格式为这样,
在这里插入图片描述
看到参数了吗,默认有一个file 为key ,关键是后面的类型,我在上传前拿到的参数格式File格式,直接在后面拿来请求,不行,我一直在这个方向努力了很久,最后没有成功,希望有经验的同学给指点一下,之后不行,晚上睡不好,各种搜资料,无意中看到这个,upload的手动上传

 console,log(this.$refs.upLoadRef)

可以看到,组件中提供了post方法,可以实现手动上传功能
在这里插入图片描述

	   所以我可以第一次走校验的接口,将上传文件给后端,第二次点击确定的时候增加参数data 然后修
	   改action 内容,使用 上传前 before-upload 方法拿到的 res 结合 post 方法,进行确认上传
	
	   于是进入又进入另一个坑,会告诉你不能修改action的值,虽然控制台报错,但是可以上传成功;
	   为了修改这个bug我用了比较蠢的方法,直接上代码

点击确认,增加参数,修改action

 /**
         * @date         2020-12-08
         * @description  确定上传
         */
        upFile(){
            this.$refs['uploadSecurityForm'].validate((valid)=>{
                if(valid){
                    //确定上传上传组件再走一次生命周期显示,因为修改action
                        this.upLoadShow = false; //用v-if来控制上传组件显示
                        this.actionUrl = '/dsmp/DataSecurityKnowledge/upload';
                        this.upLoadShow = true;
                        //等待上传组件加载完成,才能获取到upLoadRef,然后执行上传方法
                        this.$nextTick(()=>{
                            //上传需要增加文件类型参数
                            this.upOtherData.fileClass = this.addForm.fileType;
                            //触发手动上传功能
                            this.$refs.upLoadRef.post(this.fileContent);
                            //关闭上传弹窗
                            this.uploadModal = false;
                            //刷新列表
                            this.getData();
                        })
                }else{
                    return false
                }
            })
        },

总结: 在遇到问题的时候,从多个方面想办法解决,如果使用组件,建议打印组件方法 数据,有时候官方文档写的不全,所以可以查看更多

完结撒花 ★,°:.☆( ̄▽ ̄)/$:.°★

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个使用 Element UI 二次封装的 Upload 上传组件的示例代码: ``` <template> <div> <el-upload :action="uploadUrl" :headers="headers" :data="formData" :multiple="multiple" :show-file-list="showFileList" :limit="limit" :on-exceed="onExceed" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :on-progress="onProgress" > <el-button type="primary" :disabled="uploading">{{ buttonText }}</el-button> <div slot="tip" class="el-upload__tip">{{ tip }}</div> </el-upload> </div> </template> <script> export default { name: "MyUpload", props: { uploadUrl: { type: String, default: "" }, headers: { type: Object, default: () => ({}) }, formData: { type: Object, default: () => ({}) }, multiple: { type: Boolean, default: false }, showFileList: { type: Boolean, default: true }, limit: { type: Number, default: 0 }, buttonText: { type: String, default: "上传文件" }, tip: { type: String, default: "只能上传jpg/png文件,且不超过500kb" } }, data() { return { uploading: false }; }, methods: { onExceed(files, fileList) { this.$message.warning(`只能上传${this.limit}个文件`); }, beforeUpload(file) { const isJPG = file.type === "image/jpeg" || file.type === "image/png"; const isLt500K = file.size / 1024 < 500; if (!isJPG) { this.$message.error("上传图片只能是 JPG/PNG 格式!"); return false; } if (!isLt500K) { this.$message.error("上传图片大小不能超过 500KB!"); return false; } return true; }, onSuccess(response, file, fileList) { this.uploading = false; this.$emit("upload-success", response, file, fileList); }, onError(err, file, fileList) { this.uploading = false; this.$emit("upload-error", err, file, fileList); }, onProgress(event, file, fileList) { this.uploading = true; this.$emit("upload-progress", event, file, fileList); } } }; </script> <style> /* 可以根据自己的需要修改样式 */ .el-upload__tip { font-size: 14px; color: #999; margin-top: 10px; } </style> ``` 这个 Upload 组件支持以下 props: - `uploadUrl`:上传文件的接口地址 - `headers`:上传请求的 headers - `formData`:上传请求的 formData - `multiple`:是否支持多选文件 - `showFileList`:是否显示已上传文件列表 - `limit`:最多上传文件个数 - `buttonText`:上传按钮的文本 - `tip`:上传提示信息 这个 Upload 组件还支持以下事件: - `upload-success`:上传成功的回调函数,参数为 response、file 和 fileList - `upload-error`:上传失败的回调函数,参数为 err、file 和 fileList - `upload-progress`:上传进度的回调函数,参数为 event、file 和 fileList 使用示例: ``` <template> <div> <my-upload :upload-url="uploadUrl" :headers="headers" :form-data="formData" :multiple="multiple" :show-file-list="showFileList" :limit="limit" :button-text="buttonText" :tip="tip" @upload-success="onUploadSuccess" @upload-error="onUploadError" @upload-progress="onUploadProgress" ></my-upload> </div> </template> <script> import MyUpload from "@/components/MyUpload"; export default { components: { MyUpload }, data() { return { uploadUrl: "https://xxx.com/upload", headers: { token: "xxx" }, formData: { type: "avatar" }, multiple: false, showFileList: true, limit: 1, buttonText: "上传头像", tip: "只能上传jpg/png文件,且不超过500kb" }; }, methods: { onUploadSuccess(response, file, fileList) { console.log("上传成功", response, file, fileList); }, onUploadError(err, file, fileList) { console.log("上传失败", err, file, fileList); }, onUploadProgress(event, file, fileList) { console.log("上传进度", event, file, fileList); } } }; </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值