作者 RuoYi

优化文件下载出现的异常(I6DLNU)

@@ -15,9 +15,9 @@ export default { @@ -15,9 +15,9 @@ export default {
15 url: url, 15 url: url,
16 responseType: 'blob', 16 responseType: 'blob',
17 headers: { 'Authorization': 'Bearer ' + getToken() } 17 headers: { 'Authorization': 'Bearer ' + getToken() }
18 - }).then(async (res) => {  
19 - const isLogin = await blobValidate(res.data);  
20 - if (isLogin) { 18 + }).then((res) => {
  19 + const isBlob = blobValidate(res.data);
  20 + if (isBlob) {
21 const blob = new Blob([res.data]) 21 const blob = new Blob([res.data])
22 this.saveAs(blob, decodeURIComponent(res.headers['download-filename'])) 22 this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
23 } else { 23 } else {
@@ -32,9 +32,9 @@ export default { @@ -32,9 +32,9 @@ export default {
32 url: url, 32 url: url,
33 responseType: 'blob', 33 responseType: 'blob',
34 headers: { 'Authorization': 'Bearer ' + getToken() } 34 headers: { 'Authorization': 'Bearer ' + getToken() }
35 - }).then(async (res) => {  
36 - const isLogin = await blobValidate(res.data);  
37 - if (isLogin) { 35 + }).then((res) => {
  36 + const isBlob = blobValidate(res.data);
  37 + if (isBlob) {
38 const blob = new Blob([res.data]) 38 const blob = new Blob([res.data])
39 this.saveAs(blob, decodeURIComponent(res.headers['download-filename'])) 39 this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
40 } else { 40 } else {
@@ -49,9 +49,9 @@ export default { @@ -49,9 +49,9 @@ export default {
49 url: url, 49 url: url,
50 responseType: 'blob', 50 responseType: 'blob',
51 headers: { 'Authorization': 'Bearer ' + getToken() } 51 headers: { 'Authorization': 'Bearer ' + getToken() }
52 - }).then(async (res) => {  
53 - const isLogin = await blobValidate(res.data);  
54 - if (isLogin) { 52 + }).then((res) => {
  53 + const isBlob = blobValidate(res.data);
  54 + if (isBlob) {
55 const blob = new Blob([res.data], { type: 'application/zip' }) 55 const blob = new Blob([res.data], { type: 'application/zip' })
56 this.saveAs(blob, name) 56 this.saveAs(blob, name)
57 } else { 57 } else {
@@ -72,7 +72,7 @@ service.interceptors.response.use(res => { @@ -72,7 +72,7 @@ service.interceptors.response.use(res => {
72 // 获取错误信息 72 // 获取错误信息
73 const msg = errorCode[code] || res.data.msg || errorCode['default'] 73 const msg = errorCode[code] || res.data.msg || errorCode['default']
74 // 二进制数据则直接返回 74 // 二进制数据则直接返回
75 - if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){ 75 + if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
76 return res.data 76 return res.data
77 } 77 }
78 if (code === 401) { 78 if (code === 401) {
@@ -125,8 +125,8 @@ export function download(url, params, filename, config) { @@ -125,8 +125,8 @@ export function download(url, params, filename, config) {
125 responseType: 'blob', 125 responseType: 'blob',
126 ...config 126 ...config
127 }).then(async (data) => { 127 }).then(async (data) => {
128 - const isLogin = await blobValidate(data);  
129 - if (isLogin) { 128 + const isBlob = blobValidate(data);
  129 + if (isBlob) {
130 const blob = new Blob([data]) 130 const blob = new Blob([data])
131 saveAs(blob, filename) 131 saveAs(blob, filename)
132 } else { 132 } else {
@@ -228,12 +228,6 @@ export function tansParams(params) { @@ -228,12 +228,6 @@ export function tansParams(params) {
228 } 228 }
229 229
230 // 验证是否为blob格式 230 // 验证是否为blob格式
231 -export async function blobValidate(data) {  
232 - try {  
233 - const text = await data.text();  
234 - JSON.parse(text);  
235 - return false;  
236 - } catch (error) {  
237 - return true;  
238 - } 231 +export function blobValidate(data) {
  232 + return data.type !== 'application/json'
239 } 233 }