( fileName: string, url: string, params: any, data: any, loading?: NProgress | Ref<boolean>, )
| 387 | data: any, |
| 388 | loading?: NProgress | Ref<boolean>, |
| 389 | ) => Promise<any> = ( |
| 390 | fileName: string, |
| 391 | url: string, |
| 392 | params: any, |
| 393 | data: any, |
| 394 | loading?: NProgress | Ref<boolean>, |
| 395 | ) => { |
| 396 | return promise( |
| 397 | request({ |
| 398 | url: url, |
| 399 | method: 'post', |
| 400 | params, |
| 401 | data, |
| 402 | responseType: 'blob', |
| 403 | transformResponse: [ |
| 404 | function (data, headers) { |
| 405 | // 在这里可以访问 headers |
| 406 | if (data.type === 'application/json') { |
| 407 | data.text().then((text: string) => { |
| 408 | try { |
| 409 | const json = JSON.parse(text) |
| 410 | MsgError(json.message || text) |
| 411 | } catch { |
| 412 | MsgError(text) |
| 413 | } |
| 414 | }) |
| 415 | throw new Error('Response is not a valid file') |
| 416 | } |
| 417 | // const contentType = headers['content-type']; |
| 418 | const contentDisposition = headers['content-disposition'] |
| 419 | // console.log('Content-Type:', contentType); |
| 420 | // console.log('Content-Disposition:', contentDisposition); |
| 421 | // 如果没有提供文件名,则使用默认名称 |
| 422 | fileName = extractFilename(contentDisposition) || fileName |
| 423 | return data // 必须返回数据 |
| 424 | }, |
| 425 | ], |
| 426 | }), |
| 427 | loading, |
| 428 | ) |
| 429 | .then((res: any) => { |
| 430 | if (res) { |
| 431 | const blob = new Blob([res], { |
| 432 | type: 'application/octet-stream', |
| 433 | }) |
| 434 | const link = document.createElement('a') |
| 435 | link.href = window.URL.createObjectURL(blob) |
| 436 | link.download = fileName |
| 437 | link.click() |
| 438 | //释放内存 |
| 439 | window.URL.revokeObjectURL(link.href) |
| 440 | } |
| 441 | return true |
| 442 | }) |
| 443 | .catch(() => {}) |
| 444 | } |
| 445 | |
| 446 | export const download: ( |
no test coverage detected