這篇文章將為大家詳細(xì)講解有關(guān)vue項(xiàng)目中如何實(shí)現(xiàn)以blob形式導(dǎo)出文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
1、首先要確定服務(wù)器返回的數(shù)據(jù)類型。
在請求頭中加入: config.responseType = 'blob'
有時(shí)候,不是所有接口都需要該類型,則可以對接口做一個(gè)判定:
// request攔截器 service.interceptors.request.use( config => { // 根據(jù)接口判定 if ( config.url === '/setting/exportData' || config.url.indexOf('export') > -1 || config.url.indexOf('Export') > -1) { config.responseType = 'blob' // 服務(wù)請求類型 } if (getToken()) { config.headers['access_token'] = getToken() } return config }, error => { // Do something with request error // console.log(error) // for debug Promise.reject(error) } )