( fileName: string, url: string, params: any, loading?: NProgress | Ref<boolean>, )
| 288 | params: any, |
| 289 | loading?: NProgress | Ref<boolean>, |
| 290 | ) => Promise<any> = ( |
| 291 | fileName: string, |
| 292 | url: string, |
| 293 | params: any, |
| 294 | loading?: NProgress | Ref<boolean>, |
| 295 | ) => { |
| 296 | return promise( |
| 297 | request({ |
| 298 | url: url, |
| 299 | method: 'get', |
| 300 | params, |
| 301 | responseType: 'blob', |
| 302 | transformResponse: [ |
| 303 | function (data, headers) { |
| 304 | // 在这里可以访问 headers |
| 305 | if (data.type === 'application/json') { |
| 306 | data.text().then((text: string) => { |
| 307 | try { |
| 308 | const json = JSON.parse(text) |
| 309 | MsgError(json.message || text) |
| 310 | } catch { |
| 311 | MsgError(text) |
| 312 | } |
| 313 | }) |
| 314 | throw new Error('Response is not a valid file') |
| 315 | } |
| 316 | // const contentType = headers['content-type']; |
| 317 | const contentDisposition = headers['content-disposition'] |
| 318 | // console.log('Content-Type:', contentType); |
| 319 | // console.log('Content-Disposition:', contentDisposition); |
| 320 | // 如果没有提供文件名,则使用默认名称 |
| 321 | fileName = extractFilename(contentDisposition) || fileName |
| 322 | return data // 必须返回数据 |
| 323 | }, |
| 324 | ], |
| 325 | }), |
| 326 | loading, |
| 327 | ) |
| 328 | .then((res: any) => { |
| 329 | if (res) { |
| 330 | const blob = new Blob([res], { |
| 331 | type: 'application/octet-stream', |
| 332 | }) |
| 333 | const link = document.createElement('a') |
| 334 | link.href = window.URL.createObjectURL(blob) |
| 335 | link.download = fileName |
| 336 | link.click() |
| 337 | //释放内存 |
| 338 | window.URL.revokeObjectURL(link.href) |
| 339 | } |
| 340 | return true |
| 341 | }) |
| 342 | .catch(() => {}) |
| 343 | } |
| 344 | |
| 345 | export const exportExcelPost: ( |
| 346 | fileName: string, |
no test coverage detected