vue+elementUI的文件上传和文件下载

项目需求:
一个功能可以导入表格数据,还有模板提供下载参考
在这里插入图片描述
在这里插入图片描述
大致的功能需求就是这样了,上传文件的组件用的是element-UI自带的组件

配置参数:

我使用的是手动上传模块

参数说明
action必选参数,上传的地址
multiple是否支持多选文件
limit最大允许上传个数
on-exceed文件超出个数限制时的钩子
http-request覆盖默认的上传行为,可以自定义上传的实现
:before-upload上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。
file-list上传的文件列表
auto-upload是否在选取文件后立即进行上传
<!----- 上传部分 ----->
 <el-upload
            class="upload-demo"
            ref="upload"
            action=""
            name="excelFile"
            :multiple="false"
            :limit="1"
            :on-exceed="handleExceed"
            :http-request="uploadFile"
            :before-upload="beforeAvatarUpload"
            :file-list="fileList"
            :auto-upload="false"
        >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
            <div slot="tip" class="el-upload__tip">
              <p>只能上传excel文件</p> 
            </div>
        </el-upload>
		<!----- 下载按钮 ----->
        <div class="bottom">
          <span @click="download">模板下载</span>
        </div>

js方法部分:


<!----- 上传部分 ----->
handleExceed(){
      this.$notify({
        title: '警告',
        message: '只能上传一个文件哦!,若需要上传其他文件请先移除已选择的文件',
        type: 'warning'
      });
    },
    uploadFile(item){
    	//我的项目要求formdata上传数据
      const form = new FormData();
      form.append('FireAlarmDeviceSn',this.networkPage.DeviceSn)
      form.append('file',item.file)

      this.$axios.post(this.$api.ImportFireAlarmDetector,form).then(res=>{
        console.log("res",res)
        if(res.data.result.success){
          this.$message({
            message: '导入数据成功!',
            type: 'success'
          });
          this.GetDeviceDetectorList();//获取联网部件tablelist
        }else{
          this.$message({
            showClose: true,
            message: `导入失败!${res.data.result.failCause}`,
            type: 'error',
            duration:'6000'
          });
        }
        this.fileList =[]
        this.$refs.network_importDialog.dialogVisible = false
      }).catch(err=>{
        console.log("err",err)
      })

    },
    //上传之前的判断文件大小和格式
    beforeAvatarUpload(file){
      const extension = file.name.split(".")[1] === "xls";
      const extension2 = file.name.split(".")[1] === "xlsx";
      if (!extension && !extension2) {
           alert("上传模板只能是 xls、xlsx格式!");
      }
    },
    submitUpload(){
      this.$confirm('导入的方式会覆盖现有的记录, 是否继续?', '导入数据', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
      }).then(() => {
          this.$refs.upload.submit();
            
      }).catch(() => {

      });
    
    }

<!----- 下载部分 ----->
 //模板下载,后端配置好地址,只需要用a标签下载即可
    download(){
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = 'http://futest.sctsjkj.com/template/消防火警联网部件设施数据表.xlsx';
      link.click();
    },  
  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue+elementui的普通上可以使用elementui中的el-upload组件,具体步骤如下: 1. 在Vue组件中引入el-upload组件。 ``` <template> <el-upload action="https://www.example.com/upload" :headers="{Authorization: 'Bearer ' + token}" :data="{id: fileId}" :on-success="handleSuccess" :on-error="handleError" :on-progress="handleProgress"> <el-button>点击上</el-button> </el-upload> </template> <script> import {ElUpload, ElButton} from 'element-ui'; export default { components: {ElUpload, ElButton}, data() { return { token: 'your_token', fileId: 'your_file_id' }; }, methods: { handleSuccess(response, file, fileList) { console.log(response); }, handleError(error, file, fileList) { console.log(error); }, handleProgress(event, file, fileList) { console.log(event); } } }; </script> ``` 2. 在el-upload组件中设置上的地址、请求头、上数据、上成功回调、上失败回调和上进度回调等参数。 3. 在上成功回调函数中处理上成功后的逻辑。 大文件分片上可以使用如下步骤: 1. 前端将大文件分为若干个片段,并将每个片段上到后端。 2. 后端将每个片段存储在服务器上,并返回每个片段的文件名和存储路径。 3. 前端将每个片段的文件名和存储路径组成一个数组,发送给后端。 4. 后端将数组中的每个片段按照顺序合并成一个完整的文件。 5. 前端可以在上过程中显示上进度,并支持暂停、恢复和取消上。可以使用Vue+elementui+axios实现大文件分片上
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值