Vue使用Element-ui upload组件进行多文件上传(视频或者图片)


Vue使用Element-ui upload组件进行多文件上传(视频或者图片)


最近有个小的功能需求需要能够支持多文件上传图片或者视频文件。话不多说,上干货。

1、按需对接口请求进行封装(可以根据你自己的需求来)


// body传参
function bodyParams (method, url, params, contentType) {
  return new Promise((resolve, reject) => {
    Axios({
      url,
      method,
      data: contentType == 'json' ? params : qs.stringify(params),
      headers: {
        "Content-Type": contentType == 'json' ? 'application/json;charset=UTF-8' : 'application/x-www-form-urlencoded;charset=UTF-8'
      }
    }).then(
      res => {
        resolve(res);
      },
      error => {
        reject(error)
      }
    ).catch((error) => {
      reject(error);
    })
  })
}

// body传参上传文件
function bodyParams1 (method, url, params) {
  return new Promise((resolve, reject) => {
    Axios({
      url,
      method,
      data: params,
      headers: {
        "Content-Type": 'multipart/form-data'
      }
    }).then(
      res => {
        resolve(res);
      },
      error => {
        reject(error)
      }
    ).catch((error) => {
      reject(error);
    })
  })
}


export function fetchPost (url, params, contentType) {
  return bodyParams("post", url, params, contentType);
}

export function fetchPostFile (url, params, contentType) {
  return bodyParams1("post", url, params, contentType);
}

2、上传文件部分使用Element-ii upload组件

 <el-dialog
                    title="请输入处理内容"
                    :visible.sync="goDealDialogVisible"
                    width="50%"
                    :before-close="goDealHandleClose">
                <template>
                    <div>
                        <div style="margin: 10px;"></div>
                        <el-form label-width="100px">
                            <!--投诉照片上传-->
                            <el-form-item label="上传图片">
                                <el-upload
                                        action
                                        class="upload-demo"
                                        :on-preview="goDealPicHandPreview"
                                        :on-remove="goDealPicHandRemove"
                                        :file-list="picFileList"
                                        accept="file/*"
                                        multiple
                                        :auto-upload="false"
                                        :on-change="uploadFile"
                                        ref="upload">
                                    <el-button size="small" type="primary">点击上传投诉照片</el-button>
                                </el-upload>
                            </el-form-item>

                            <!--投诉视频上传-->
                            <el-form-item label="上传视频">
                                <!--<el-input  placeholder="请输入内容"></el-input>-->
                                <template slot-scope="scope">
                                    <el-upload class="avatar-uploader el-upload--text"
                                               action
                                               :show-file-list="true"
                                               :on-preview="videoHandlePreview"
                                               :on-remove="videoHandleRemove"
                                               :file-list="videoFileList"
                                               accept="file/*"
                                               :on-success="handleVideoSuccess"
                                               :before-upload="beforeUploadPicVideo"
                                               multiple
                                               :auto-upload="false"
                                               :on-change="uploadFile"
                                               ref="upload">
                                        <i class="el-icon-plus avatar-uploader-icon"></i>
                                        <el-progress v-if="videoFlag == true"
                                                     type="circle"
                                                     :percentage="videoUploadPercent"
                                                     style="margin-top:30px;"></el-progress>
                                        <el-button class="video-btn"
                                                   slot="trigger"
                                                   size="small"
                                                   v-if="isShowUploadVideo"
                                                   type="primary">选取文件
                                        </el-button>
                                    </el-upload>
                                </template>
                                <P v-if="isShowUploadVideo"
                                   class="text">请保证视频格式正确,且不超过20M</P>
                            </el-form-item>

                            <!--投诉处理结果-->
                            <el-form-item label="处理结果">
                                <el-input v-model="reportingResult" type="textarea" placeholder="请输入投诉处理结果"></el-input>
                            </el-form-item>
                        </el-form>
                    </div>
                </template>

                <span slot="footer" class="dialog-footer">
        <el-button @click="goDealDialogVisible = false,cancelBtn">取 消</el-button>
        <el-button type="primary" @click="determineBtn">确 定</el-button>
        </span>
            </el-dialog>

其中:
组件上的on-preview、on-remove、before-upload、on-success等方法根据自己的需求进行书写

3、使用自己封装好的方法进行接口请求调用

            determineBtn () {
                this.goDealDialogVisible = false
                this.formData.append('id', this.reportId);
                this.formData.append('reportingResult', this.reportingResult);
                this.$fetchPostFile('你的后台接口请求地址', this.formData).then(response => {
                    // this.getComplaintManagementInfo();//调数据接口方法,获取投诉数据信息
                }).catch(function (error) {
                    console.log(error);
                })
                this.$message({
                    message: '上传成功!!!',
                    type: 'success',
                    center: true,
                    showClose: true,
                    offset: 50,
                })

            },

4、进行数据测试
在这里插入图片描述接口请求成功
在这里插入图片描述上述列出了关键核心代码,如有不对之处,望大佬指正!!

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用vueelement-ui制作上传文件的效果时,可以按照以下步骤进行: 1. 安装element-ui:在终端中使用npm或yarn进行安装,例如:npm install element-ui --save。 2. 在vue组件中引入element-ui的上传组件:在需要使用上传功能的组件中,使用import引入element-ui的上传组件,例如: ``` <template> <div> <el-upload action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :file-list="fileList" multiple list-type="picture" :auto-upload="false" :on-success="handleUploadSuccess" :on-error="handleUploadError"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> <script> import { Upload, Button } from 'element-ui'; export default { components: { 'el-upload': Upload, 'el-button': Button }, data() { return { fileList: [] }; }, methods: { handlePreview(file) { console.log('preview', file); }, handleRemove(file) { console.log('remove', file); }, 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文件'); } if (!isLt500k) { this.$message.error('文件大小不能超过500kb'); } return isJPG && isLt500k; }, handleUploadSuccess(response, file, fileList) { console.log('success', response, file, fileList); }, handleUploadError(error, file, fileList) { console.log('error', error, file, fileList); } } }; </script> ``` 3. 配置上传组件的属性和方法:在引入上传组件后,需要根据实际需求进行配置,例如:设置上传的地址、文件大小限制、文件类型限制、上传前的校验、上传成功和失败的回调函数等。 4. 在模板中使用上传组件:在组件的模板中使用上传组件,并将需要上传的文件绑定到fileList属性上,例如:`:file-list="fileList"`。 5. 样式美化:根据实际需求对上传组件的样式进行美化,例如:设置上传按钮的颜色、设置文件列表的样式等。 通过以上步骤,就可以在vue项目中使用element-ui制作上传文件的效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值