1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { RequestFile } from './request-file'
- import { downloadByStream, getFileNameFormHeader } from './util'
- export class DownloadFile extends RequestFile {
- constructor ({ type, method = 'get', apiName, url, params = {}, data, config = {}, pathParams = {}, headers = {} }) {
- method = type || method
- super({ method, apiName, url, data, pathParams, params, config, headers })
- this.config.responseType = 'blob'
- this.cancelMessage = '已取消下载'
- }
- async send () {
- try {
- const res = await this.request()
- const filenameISO = getFileNameFormHeader(res.headers['content-disposition'])
- const filename = this.config.encodeType === 'iso' ? decodeURI(escape(filenameISO)) : filenameISO//
- downloadByStream(res.data, filename)
- } catch (err) {
- this.notifyError(err)
- throw err
- }
- }
- }
- // export class DownloadFile {
- // constructor ({ type = 'get', apiName, url, params = {}, data, config = {}, pathParams = {}, headers = {} }) {
- // this.type = type.toLocaleLowerCase()
- // this.path = getRequestPath(apiName, url, pathParams)
- // config.timeout = 0
- // config.params = params
- // this.cancel = () => {} // noop
- // this.abort = this.abort.bind(this) // 显示的绑定
- // const _this = this
- // config.cancelToken = new CancelToken(function (c) {
- // _this.cancel = c
- // })
- // // download特有
- // this.data = data
- // config.headers = Object.assign({}, { 'Content-Type': 'application/octet-stream;charset=UTF-8' }, config.headers, headers)
- // config.responseType = 'blob'
- //
- // this.config = config
- // }
- //
- // send () {
- // return new Promise((resolve, reject) => {
- // // put,post第二个参数为data
- // const params2 = ['put', 'post'].includes(this.type) ? this.data : this.config
- // axios[this.type](this.path, params2, this.config).then(res => {
- // const filenameISO = getFileNameFormHeader(res.headers['content-disposition'])
- // const filename = this.config.encodeType === 'iso' ? decodeURI(escape(filenameISO)) : filenameISO//
- // downloadByStream(res.data, filename)
- // resolve()
- // }).catch(async err => {
- // const message = await notifyErrorMessage(err)
- // CipMessage({ type: 'error', message })
- // reject(err)
- // })
- // })
- // }
- //
- // abort () {
- // this.cancel('已取消下载')
- // }
- // }
|