• 流文件下载方法


    重要知识点:

    FileReader.readAsText()

    readAsText 方法可以将 Blob 或者 File 对象转根据特殊的编码格式转化为内容(字符串形式)

     
    const download = (res, mime) => {
        const mimeList = {
          pdf: 'application/pdf',
          jpg: 'image/jpeg',
          jpeg: 'image/jpeg',
          gif: 'image/gif',
          png: 'image/png',
          doc: 'application/msword',
          docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
          ppt: 'application/vnd.ms-powerpoint',
          pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
          xls: 'application/vnd.ms-excel',
          xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          zip: 'application/zip',
          rar: 'application/x-rar-compressed',
          bmp: 'image/bmp',
          tif: 'image/tiff',
        }
        if (res && res.type.indexOf('application/json') > -1) {
          const reader = new FileReader()
          reader.onload = () => {
            if (reader.result) {
              const result = JSON.parse(reader.result)
              Vue.prototype.$message.error(result.desc)
            }
          }
          reader.readAsText(res, 'utf-8')
          return
        }
        const blob = new Blob([res], { type: mimeList[mime] || 'application/vnd.ms-excel' })
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
          window.navigator.msSaveOrOpenBlob(blob, downloadName)
        } else {
          const blobURL = window.URL.createObjectURL(blob)
          const tempLink = document.createElement('a')
          tempLink.style.display = 'none'
          tempLink.href = blobURL
          tempLink.setAttribute('download', downloadName)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          document.body.appendChild(tempLink)
          tempLink.click()
          document.body.removeChild(tempLink)
          setTimeout(() => {
            window.URL.revokeObjectURL(blobURL)
          }, 50)
        }
      }
  • 相关阅读:
    Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) C. Success Rate
    Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt
    go 读取数据库所有表数据显示到网页(便于测试时看)
    go 文件上传
    go 文件服务(读取须权限)
    vps git无法clone DNS解析错误
    go 自己封的postgresql连接池操作包
    看 迪杰斯特拉(Dijsktra)算法体会
    Floyd算法(弗洛伊德算法) 百度百科
    Constructing Roads In JGShining's Kingdom
  • 原文地址:https://www.cnblogs.com/xcdl/p/15241232.html
Copyright © 2020-2023  润新知