Commit 81a01a1d9d20fcc39db27572423a80c6dfc6755d

Authored by RuoYi
1 parent 6523fe59

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

ruoyi-ui/src/plugins/download.js
... ... @@ -15,9 +15,9 @@ export default {
15 15 url: url,
16 16 responseType: 'blob',
17 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 21 const blob = new Blob([res.data])
22 22 this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
23 23 } else {
... ... @@ -32,9 +32,9 @@ export default {
32 32 url: url,
33 33 responseType: 'blob',
34 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 38 const blob = new Blob([res.data])
39 39 this.saveAs(blob, decodeURIComponent(res.headers['download-filename']))
40 40 } else {
... ... @@ -49,9 +49,9 @@ export default {
49 49 url: url,
50 50 responseType: 'blob',
51 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 55 const blob = new Blob([res.data], { type: 'application/zip' })
56 56 this.saveAs(blob, name)
57 57 } else {
... ...
ruoyi-ui/src/utils/request.js
... ... @@ -72,7 +72,7 @@ service.interceptors.response.use(res => {
72 72 // 获取错误信息
73 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 76 return res.data
77 77 }
78 78 if (code === 401) {
... ... @@ -125,8 +125,8 @@ export function download(url, params, filename, config) {
125 125 responseType: 'blob',
126 126 ...config
127 127 }).then(async (data) => {
128   - const isLogin = await blobValidate(data);
129   - if (isLogin) {
  128 + const isBlob = blobValidate(data);
  129 + if (isBlob) {
130 130 const blob = new Blob([data])
131 131 saveAs(blob, filename)
132 132 } else {
... ...
ruoyi-ui/src/utils/ruoyi.js
... ... @@ -228,12 +228,6 @@ export function tansParams(params) {
228 228 }
229 229  
230 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 }
... ...