vue组件 文件的上传和下载

参考代码会放到最后

1.上传

用el-upload组件上传文件

<el-upload
        class="upload-file"
        action="xxxxxx/xxxxxxx/xxxxxxxx"
        :headers="xxxxxx"
        :before-upload="beforeUploadFile"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="1"
        :on-exceed="handleExceed"
        :on-success="uploadSuccess"
        :file-list="fileList"
        :data="uploadParams">
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">请上传图片、表格或文档,大小不得超过5M</div>
</el-upload>
  • action中填写文件上传的接口地址,如果上传需要携带header信息,在:headers="xxxxxx"中填写。
  • :file-list是已上传的文件列表。
  • uploadParams是我自己定义的参数,因为我的文件上传接口action="xxx"需要一个额外的参数,如果你的文件接口不需要参数, :data里就不用写了,空引号就行。
  • :limit 限制文件个数。

参数定义:

fileList: [],
confirmForm: {
   
     fileUrl:'',  // 记录文件上传成功后的下载连接
     fileName:'', // 记录文件的名字
 },
uploadParams:{
   
     fileName:'' // 我的上传接口需要一个fileName的入参,如果还需要其他入参,可以一并加到uploadParams对象里
 },
  • 注意:this.confirmForm、this.uploadParams是我自己额外定义的参数,因为我的功能只需要上传一个文件就够了,并且我的文件上传接口会返回给我两个参数“fileUrl”和“fileName”,所以我直接用自定义的参数记录下文件的信息。这一块需要各位根据自己业务需要修改。
  • 如果需要多个文件,要么直接用已经定义好的:file-list=“fileList”,从fileList中去取文件列表。

相关方法:

			//文件上传之前
             beforeUploadFile(file) {
   
                const size = file.size / 1024/ 1024
                if (size > 5) {
   
                    this.$message({
   
                    message: '文件大小不得大于5M',
                    duration: 2000,
                    showClose: true,
                    type: 'warning'
                  });
                  return false;
                }

               var FileExt = file.name.replace(/.+\./, "");
               if (["xls", "xlsx","pdf","jpg","jpeg","png","doc","docx","txt"].indexOf(FileExt.toLowerCase()) === -1) {
   
                  this.$message({
   
                    message: '上传文件只能是图片、表格或文档',
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件上传下载前端开发中非常常见的功能,Vue提供了方便的方法来实现这些功能。 文件上传Vue中可以使用第三方库vue-file-upload来实现文件上传,具体步骤如下: 1. 安装vue-file-upload ```sh npm install vue-file-upload --save ``` 2. 在组件中引入并注册vue-file-upload ```js import Vue from 'vue'; import VueFileUpload from 'vue-file-upload'; Vue.use(VueFileUpload); ``` 3. 在模板中使用vue-file-upload来实现文件上传 ```html <template> <div> <vue-file-upload url="/api/upload" @success="onUploadSuccess" @error="onUploadError" @progress="onUploadProgress" ></vue-file-upload> </div> </template> ``` 其中,url属性指定上传文件的地址,success、error和progress分别指定上传成功、上传失败和上传进度的回调函数。 文件下载Vue中可以使用原生的XMLHttpRequest对象或者第三方库axios来实现文件下载,具体步骤如下: 1. 使用XMLHttpRequest对象下载文件 ```js const xhr = new XMLHttpRequest(); xhr.open('GET', '/api/download', true); xhr.responseType = 'blob'; xhr.onload = () => { if (xhr.status === 200) { const blob = new Blob([xhr.response], { type: 'application/octet-stream' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = 'file.txt'; link.click(); URL.revokeObjectURL(url); } }; xhr.send(); ``` 其中,responseType属性指定响应类型为blob,onload事件中将响应数据转为Blob对象,并创建一个超链接来下载文件。 2. 使用axios下载文件 ```js axios({ method: 'get', url: '/api/download', responseType: 'blob', }).then((response) => { const blob = new Blob([response.data], { type: 'application/octet-stream' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = 'file.txt'; link.click(); URL.revokeObjectURL(url); }); ``` 其中,responseType属性指定响应类型为blob,然后将响应数据转为Blob对象,并创建一个超链接来下载文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值