亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript使用Blob對(duì)象實(shí)現(xiàn)的下載文件操作示例

 更新時(shí)間:2020年04月18日 12:54:00   作者:harmsworth2016  
這篇文章主要介紹了javascript使用Blob對(duì)象實(shí)現(xiàn)的下載文件操作,結(jié)合實(shí)例形式分析了javascript使用Blob對(duì)象下載文件相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了javascript使用Blob對(duì)象實(shí)現(xiàn)的下載文件操作。分享給大家供大家參考,具體如下:

Blob對(duì)象


Blob是一個(gè)類文件的不可變的原始數(shù)據(jù)對(duì)象,非javascript原生數(shù)據(jù)類型,F(xiàn)ile對(duì)象就是繼承自Blob對(duì)象,且在Blob的基礎(chǔ)上進(jìn)行擴(kuò)展,以便支持用戶系統(tǒng)上的文件。

前言

最近在做以post請(qǐng)求方式導(dǎo)出excel時(shí),想到了可以使用Blob對(duì)象將后臺(tái)返回的輸出流以arraybuffer或blob的格式接收交給Blob處理,最后使用URL生成鏈接,供瀏覽器下載excel。

環(huán)境

  1. vue2.x
  2. webpack3.x
  3. axios

操作

import axios from 'axios'
/**
* 從服務(wù)器下載excel
*/
function downloadExcel (settings) {
 const defaultOptions = {
  responseType: 'arraybuffer'
 }
 Object.assign(settings.options, defaultOptions)
 requestToResponse(settings).then(res => {
  const filename = decodeURI(res.headers['content-disposition'].split('filename=')[1])
  if ('download' in document.createElement('a')) {
   downloadFile(res.data, filename)
  } else {
   Message.error('瀏覽器不支持')
  }
 })
}
/**
* 發(fā)送http請(qǐng)求
* @param {Object} settings api參數(shù)
* @returns reponse
*/
function requestToResponse (settings) {
  const defaultParams = {
  timeout: 45000,
  headers: {
   'X-Requested-With': 'XMLHttpRequest',
   'Content-Type': 'application/json'
  },
  responseType: 'json',
  method: 'POST'
 }
 Object.assign(defaultParams, settings)
  return new Promise((resolve, reject) => {
  axios(defaultParams).then(res => {
   resolve(res)
  }).catch(err => {
   reject(err)
  })
 })
}
/**
* blob下載(兼容IE)
* @param {String} content 文件內(nèi)容
* @param {String} filename 文件名
*/
function downloadFile (content, filename) {
 const blob = new Blob([content])
 // IE
 if (window.navigator && window.navigator.msSaveOrOpenBlob) {
  window.navigator.msSaveOrOpenBlob(blob, filename)
 } else {
  imatateDownloadByA(window.URL.createObjectURL(blob), filename)
 }
}
/**
* 通過a標(biāo)簽?zāi)M下載
* @param {String} href
* @param {String} filename
*/
function imatateDownloadByA (href, filename) {
 const a = document.createElement('a')
 a.download = filename
 a.style.display = 'none'
 a.href = href
 document.body.appendChild(a)
 a.click()
 a.remove()
 window.URL.revokeObjectURL(href)
}

// 下載excel
downloadExcel({
  url: '/default/excel/export',
  responseType: 'arraybuffer'
})

responseType設(shè)置為arraybuffer
在這里插入圖片描述
responseTyp設(shè)置成blob
在這里插入圖片描述
不設(shè)置responseType,出現(xiàn)亂碼
在這里插入圖片描述
若引入mockjs,其攔截響應(yīng),重置了responseType,會(huì)出現(xiàn)亂碼
在這里插入圖片描述

總結(jié)

  1. 此下載excel只適用于post請(qǐng)求,get請(qǐng)求交給瀏覽器自行解析處理
  2. responseType必須設(shè)置成arraybuffer或blob
  3. 下載excel時(shí)務(wù)必關(guān)閉mockjs之類的攔截響應(yīng)的服務(wù)

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運(yùn)行效果。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論