Complete attachment upload feature

pull/3445/head
johnniang 2019-04-15 18:32:43 +08:00
parent b771a3be5f
commit b82a4ac2d1
3 changed files with 49 additions and 2 deletions

View File

@ -1,3 +1,4 @@
import axios from 'axios'
import service from '@/utils/service' import service from '@/utils/service'
const baseUrl = '/admin/api/attachments' const baseUrl = '/admin/api/attachments'
@ -26,4 +27,22 @@ attachmentApi.delete = attachmentId => {
}) })
} }
attachmentApi.CancelToken = axios.CancelToken
attachmentApi.isCancel = axios.isCancel
attachmentApi.upload = (formData, uploadProgress, cancelToken) => {
return service(
{
url: `${baseUrl}/upload`,
timeout: 8640000, // 24 hours
data: formData, // form data
onUploadProgress: uploadProgress,
method: 'post'
},
{
cancelToken: cancelToken
}
)
}
export default attachmentApi export default attachmentApi

View File

@ -35,7 +35,8 @@
<a-upload-dragger <a-upload-dragger
name="file" name="file"
:multiple="true" :multiple="true"
action="http://localhost:8090/admin/api/attachments/upload" accept="image/*"
:customRequest="handleUpload"
@change="handleChange" @change="handleChange"
> >
<p class="ant-upload-drag-icon"> <p class="ant-upload-drag-icon">
@ -217,6 +218,28 @@ export default {
}, },
onChildClose() { onChildClose() {
this.drawerVisible = false this.drawerVisible = false
},
handleUpload(option) {
this.$log.debug('Uploading option', option)
const CancelToken = attachmentApi.CancelToken
const source = CancelToken.source()
const data = new FormData()
data.append('file', option.file)
attachmentApi
.upload(data, source.token, option.onProgress)
.then(response => {
option.onSuccess(response, option.file)
})
.catch(error => {
option.onError(error, error.response)
})
return {
abort: () => {
source.cancel('Upload operation canceled by the user.')
}
}
} }
} }
} }

View File

@ -124,7 +124,12 @@
> >
<a-row type="flex" align="middle"> <a-row type="flex" align="middle">
<a-col :span="24"> <a-col :span="24">
<div class="attach-item" v-for="(item, index) in attachments" :key="index" @click="showDetailDrawer(item)"> <div
class="attach-item"
v-for="(item, index) in attachments"
:key="index"
@click="showDetailDrawer(item)"
>
<img :src="item.thumbPath"> <img :src="item.thumbPath">
</div> </div>
</a-col> </a-col>