element 如何实现文件上传下载导出

上传:

前端:

<div>
  <el-dialog
      title="低值易耗文件上传"
      :visible.sync="dialogUploadVis"
      width="25%"
    >
      <el-upload
        class="upload-demo"
        drag
        :on-change="handleChange"
        :file-list="upload.fileList"
        :multiple="false"
        :auto-upload="false"
        :http-request="uploadFile"
        action="string"
        ref="upload"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip" style="text-align: center;">
          仅允许导入xlsx、xls格式文件。
          <el-link type="primary" :underline="false">下载模板</el-link>
        </div>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="uploadButSubmit">确 定</el-button>
        <el-button @click="dialogUploadVis = false">取 消</el-button>
      </span>
    </el-dialog>
</div>



<script>
data(){
return(
upload: {
        fileList: [],
        formData: ''
      },
)} 

 methods: {
  submitUpload() {
      this.$refs.upload.submit()
    },
    delFile() {
      this.fileList = []
    },
    handleChange(file, fileList) {
      this.fileList = fileList
    },
    //自定义上传文件
    uploadFile(file) {
      //this.formData.append("file", file.file);
    },
    //保存按钮
    async uploadButSubmit() {
      let formData = new FormData()
      console.log(this.fileList)
      formData.append('file', this.fileList[0].raw) //拿到存在fileList的文件存放到formData中
      await ****(formData)
        .then(res => {
          this.$message({
            message: res,
            type: 'success'
          })
          this.$emit('closeUpload')
        })
        .catch(res => {
          this.$message({
            message: res,
            type: 'error'
          })
        })
    },}
</script>

下载:

前端:/static/filename.pdf 文件应该放在项目的 public 目录下。当应用部署后,这个文件会被作为静态资源服务。

<template>
  <a href="/static/filename.pdf" download>下载文件</a>
</template>

导出:

前端:

<div>
<el-button type="warning" @click="downloadBut"
            ><i class="el-icon-download"></i> 导出</el-button>
</div>

//api
//requestMultipart 为文件请求方式
export function ****() {
  return requestMultipart({
    url: '*****',
    method: 'get',
    params: {
    *****
    },
    responseType: 'blob'
  })
}

//npm install file-saver --save
import FileSaver from 'file-saver'

<script>
    async downloadBut() {
      await *****()
        .then(res => {
          FileSaver.saveAs(
            new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' }),
            `低值易耗.xlsx`
          )
        })
        .catch()
    },
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_文和_

感觉对您有用,可以来点鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值