vue element 文件上传(单文件、多文件手动上传) 、 文件下载

这篇博客简单记录了一次使用Vue和Element实现文件上传(包括单文件和多文件)及文件下载的操作过程,主要展示了HTML和JS代码片段,旨在积累个人技术知识。
摘要由CSDN通过智能技术生成
一、文件上传
记一次文件上传和文件下载,写的比较仓促,就记录了代码过程,其他没有详细解释,只为记录自己知识积累的过程。
1、单文件上传

HTML部分:

 <el-row v-for="(item,index) in DebugfileList">
        <el-col :span="10"><span>{
   {
   item.name}}</span></el-col>
        <el-col :span="14">
          <el-upload 
            class="upload-demo"
            action="#" 
            :data="{name:item.businessType}" 
            :on-remove="handleRemove"
            :before-remove="beforeRemove" 
            :limit="1" 
            :on-exceed="handleExceed" 
            :file-list="item.fileList"
            :http-request="handelUploadDebugfile" 
            style="display:flex;flex-direction:row;">
            <el-button size="small" type="primary" icon="el-icon-document-add">选取文件</el-button>
          </el-upload>
        </el-col>
        </el-upload>
      </el-row>

JS部分:

 // 单文件调试附件上传
      handelUploadDebugfile(e) {
   
        let file = e.file;
        let form = new FormData()
        let maxType = '1';
        let businessType = e.data.name;
        form.append('files', e.file);
        uploadfile(form, this.orderId, maxType, businessType).then(data => {
   
          this.getDeFilesTableData()
        })
      },//这里传了四个参数,files,orderId,maxType,businessType
2、多文件上传

HTML部分:

 <el-dialog title="上传文件" :visible.sync="dialogFile" width="50%">
      <div class="item list">
        <div>
          <el-row>
            <el-col :span="3"><span> 文件类型选择:</span>
            </el-col>
            <el-col :span="5">
              <el-select v-model="fileTypevalue" placeholder="请选择文件类型">
                <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8" />
            <el-col :span="8" />
          </el-row>
          <el-row>
            <el-col :span="3"><span> 上传文件:</span></el-col>
            <el-col :span="5">
              <el-upload 
                action="#" 
                ref="upload"  
                :before-upload="beforeupload" 
                :on-preview="handlePreview" 
                :on-remove="handleRemove" 
                multiple
                :file-list="mutiFileList" 
                :auto-upload="false" 
                :http-request="mutiFilesUpload">
                <el-button slot="trigger" size="small" type="primary" icon="el-icon-folder-add">上传附件</el-button>
              </el-upload>
            </el-col>
            <el-col :span="8" />
            <el-col :span="8" />
          </el-row>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFile = false">取 消</el-button>
        <el-button type="primary" icon="el-icon-upload" @click="submitFiles">上传到服务器</el-button>
      </span>
    </el-dialog>

JS部分:


      // 多文件附件上传到服务器
      mutiFilesUpload(e) {
   
        this.Filesform.append('files', e.file)
      },
      // 多文件附件上传到服务器
      submitFiles() {
   
          this.Filesform = new FormData()
          this.$refs.upload.submit()
          let maxType = '2';
          let businessType = this.fileTypevalue;
          let form = this.Filesform
        if (this.fileTypevalue != '' && this.flag) {
   
          uploadfile(form, this.orderId, maxType, businessType).then(data => {
   
            this.dialogFile = false
            this.fileTypevalue = ''
            this.$refs.upload.clearFiles()
            this.flag=false
            this.getFilesTableData()
          })
        } else {
   
          this.$notify({
   
            title: '警告',
            message: '请选择文件类型或文件',
            type: 'warning'
          });
        }
      },
一、文件下载

HTML部分:

 <el-button size="mini" type="warning" icon="el-icon-download" @click="download(row)">下载</el-button>

JS部分:

 download(row) {
   
        let id = row.id
        var elemIF = document.createElement('iframe');
        elemIF.src = '/api/produce/workFileInfo/downloadFile?id=' + id;
        elemIF.style.display = 'none';
        document.body.appendChild(elemIF);
      },
附:整个页面代码
<template>
    <div class="main">
    <!-- 单文件调试附件 -->
    <div class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值