• 前端文件下载(html5+vue/excel下载)


    excel等文件的上传和下载

    一、下载

    1.data数据

    data () {
        return {
          list:[{name: 'cs1', id: '5'}, {name: 'cs1', id: '24'}],
          selectModule: '', // 选择某列表中一个
          selectModuleName: '', // 下载的名称
          selectModuleUrl: '' // 下载的地址
        }
    }

    2.Dom操作

    <div>
      <span>下载模版:</span>
      <el-select v-model="selectModule" @change="handleSelectModule" placeholder="请选择下载模板" style="margin-left: 20px;  300px; height: 36px;">
        <el-option v-for="item in list" :key="item.id" :value="item.id" :label="item.name" />
      </el-select>
      <a :href="selectModuleUrl" :download="selectModuleName">下载</a>
    </div>

    3.下载

    handleSelectModule (val) {
      // 获取模版下载地址
      const params = {
        fileId: val,
        type: 1
      }
      downLoadExcel(params, '下载地址').then(res => {
        var reader = new FileReader()
        reader.readAsText(res.data, 'gbk')
        const _this = this
        reader.onload = function (e) {
          try {
            var result = JSON.parse(reader.result)
            console.log(result)
            _this.$message({
              type: 'error',
              message: '下载模版不存在'
            })
          } catch (err) {
            const url = window.URL.createObjectURL(res.data)
            let proName = ''
            for (let i = 0; i < _this.list.length; i++) {
              if (_this.list[i].proID === val) {
                proName = _this.list[i].proName
                break
              }
            }
            _this.selectModuleUrl = url
            _this.selectModuleName = `${proName}下载模版名称.xls`
          }
        }
      }).catch(error => {
        console.log(error)
      })
    },
    
    // 下载方法
    import axios from 'axios'
    
    function downLoadExcel (params, url) {
      console.log(params, url)
      return axios({
        baseURL: `${API_ROOT}`, // 下载的基本地址, url是拼接下载地址(后缀), 如 http://www.baidu.com/downUrl
        withCredentials: true,
        crossDomain: true,
        url: url,
        method: 'post',
        responseType: 'blob',
        params: params
      })
    }

    二、上传

    1.data数据data () {    return {

          size: null, // 文件大小
          fileInfo: {
            name: '',
            file: undefined
          },
          id: ''
          dialogVisible: false,
        }
    }

    2.操作dom

       <input ref="fileUpload" type="file" @change="fileUpload($event)" style=" 0px; height: 0px;visibility: hidden;"/>
        <span v-if="!(fileInfo.name)" @click="handleCheckFile" class="opItem">请选择上传类型</span>
        <span class="opItem">{{fileInfo.name}}</span>
        <div class="dialog-content">
         <el-select filterable v-model="selectProject" @change="handleSelectModule" placeholder="请选择上传项目" style="margin-left: 20px;  300px; height: 36px;">
          <el-option v-for="item in list" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>

    3.方法实现

      handleSelectModule () {
        // 调用下载方法
        如上
      }
      handleCheckFile () {
        this.$refs.fileUpload.click()
      },
      fileUpload (e) {
        // console.log(e.target.files[0])
        // let type = e.target.files[0].name.split('.')[1]
        this.fileInfo.name = e.target.files[0].name
        this.fileInfo.file = e.target.files[0]
        this.size = e.target.files[0].size
        // this.size = bigSize / 1024;
        this.$refs.fileUpload.value = null
      },
      uploadList () {
          if (this.size > 30 * 1024 * 1024) {
            this.$message({
              type: 'error',
              message: '文件不能超过30M'
            })
            return
          }
          this.dialogVisible = false
          uploadFileAsync({ file: this.fileInfo.file, id: this.id, url: '/xxxx/uploadExcel' })
            .then(res => {
             console.log('---成功操作')
            })
            .catch(error => {
              this.fileInfo = {
                name: '',
                file: undefined
              }
              this.$message({
                type: 'error',
                message: error.responseMsg
              })
            })
        },
      function uploadFileAsync (params) {
      const { file, id, url } = params
      var formData = new FormData()
      formData.append('uploadFile', file)
      return axios({
        baseURL: `${API_ROOT}`,
        url: url,
        data: formData,
        timeout: 600000,
        method: 'post',
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        withCredentials: true,
        crossDomain: true,
        params: {
          id: id
        }
      }).then(res => {
        return Promise.resolve(res.data)
      }).catch(error => {
        console.log(error)
        return Promise.reject(new Error({ responseCode: '20089', responseMsg: '请求出错了,请稍后再试' }))
      })

     

  • 相关阅读:
    数据库学习 ORA12545:因目标主机或对象不存在,连接失败
    Oracle创建命名空间和新用户
    创建一个命名空间
    修改用户密码
    Oracle启动
    数据库表中列类型的修改和ALTER的用法
    修改列类型
    IE6/IE7/IE8/Firefox/Chrome/Safari的CSS hack兼容一览表
    css:fixed定位兼容不同系列不同版本的浏览器包括IE6.0
    jQuery Google Charts一个封装google chart api的jquery插件 饼状图
  • 原文地址:https://www.cnblogs.com/TheYouth/p/14151642.html
Copyright © 2020-2023  润新知