• 记 js 上传下载细节


    上传

    <input type="file" ref="inputFile" accept=".xls,.xlsx" @change="readFile($event)" style="display: none" />

    js

     //上传
        async readFile(e) {
          const files = e.target.files
          if (files.length <= 0) {
            return
          } else if (!/(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
            this.$message.warning('上传格式不正确,请上传xls、xlsx格式')
          }
          const form = new FormData()
          form.append('risenUpload', files[0])
          let result = await demoUpload(form)
          if (result.success) {
            this.$message.success('上传成功')
          } else {
            this.$message.error(result.actionErrors[0])
          }
        },

    上传文件需要添加

     headers: {
          'Content-Type': 'multipart/form-data'
     },

    下载

    1.  window.open    方法

      window.open(接口地址, '_self')

    2. 创建a标签点击 下载  切记 接口 配置添加 responseType 

    export function downDemoTemplate(params) {
      return request({
        url: '/downDemoTemplate',
        method: 'get',
        responseType: 'blob',
        params
      })
    }



    downDemoTemplate().then(res => { let blob = new Blob([res]) let fileName = '范例.xls' if (typeof window.navigator.msSaveBlob !== 'undefined') { window.navigator.msSaveBlob(blob, fileName) } else { let URL = window.URL || window.webkitURL let objectUrl = URL.createObjectURL(blob) if (fileName) { var a = document.createElement('a') if (typeof a.download === 'undefined') { window.location = objectUrl } else { a.href = objectUrl a.download = fileName document.body.appendChild(a) a.click() a.remove() } } else { window.location = objectUrl } } })
  • 相关阅读:
    英语长难句
    服务器部署 halo博客项目
    11月迟来的总结
    10月总结
    9月总结
    python根据字符串导入模块
    RestFul(番外):类视图更适合restful
    Django-基础 Meta自定义
    (垃圾代码)修改同目录下面的xml文件标签数值
    Django-templatetags设置(在templates中使用自定义变量)
  • 原文地址:https://www.cnblogs.com/wxyblog/p/16401595.html
Copyright © 2020-2023  润新知