Complete cancelling uploading feature

pull/9/head
johnniang 2019-04-15 20:01:34 +08:00
parent ad6ffeb54d
commit b7d1334a0e
3 changed files with 16 additions and 13 deletions

View File

@ -31,18 +31,14 @@ attachmentApi.CancelToken = axios.CancelToken
attachmentApi.isCancel = axios.isCancel attachmentApi.isCancel = axios.isCancel
attachmentApi.upload = (formData, uploadProgress, cancelToken) => { attachmentApi.upload = (formData, uploadProgress, cancelToken) => {
return service( return service({
{ url: `${baseUrl}/upload`,
url: `${baseUrl}/upload`, timeout: 8640000, // 24 hours
timeout: 8640000, // 24 hours data: formData, // form data
data: formData, // form data onUploadProgress: uploadProgress,
onUploadProgress: uploadProgress, cancelToken: cancelToken,
method: 'post' method: 'post'
}, })
{
cancelToken: cancelToken
}
)
} }
export default attachmentApi export default attachmentApi

View File

@ -29,6 +29,12 @@ service.interceptors.response.use(
}, },
error => { error => {
NProgress.done() NProgress.done()
if (axios.isCancel(error)) {
Vue.$log.debug('Cancelled uploading by user.')
return Promise.reject(error)
}
Vue.$log.error('Response failed', error) Vue.$log.error('Response failed', error)
const response = error.response const response = error.response

View File

@ -127,6 +127,7 @@
import { PageView } from '@/layouts' import { PageView } from '@/layouts'
import { mixin, mixinDevice } from '@/utils/mixin.js' import { mixin, mixinDevice } from '@/utils/mixin.js'
import attachmentApi from '@/api/attachment' import attachmentApi from '@/api/attachment'
import axios from 'axios'
export default { export default {
components: { components: {
PageView PageView
@ -226,7 +227,7 @@ export default {
}, },
handleUpload(option) { handleUpload(option) {
this.$log.debug('Uploading option', option) this.$log.debug('Uploading option', option)
const CancelToken = attachmentApi.CancelToken const CancelToken = axios.CancelToken
const source = CancelToken.source() const source = CancelToken.source()
const data = new FormData() const data = new FormData()