upload上传至oss 2020-06-22- 2021-10-29 前端 > vue > ant-design123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144/*** _utils中的两个方法*/// 获取默认的原组件中的propsexport const getDefaultProps = (componentProps, vm) => { const props = Object.keys(componentProps).reduce((pre, cur) => { const stash = {} if (vm[cur]) { stash[cur] = vm[cur] } return { ...pre, ...stash } }, {}) return props}// 获取文件名export const fixFileSuffix = (fileName, oriFileName) => { const suffix = oriFileName.substring(oriFileName.lastIndexOf('.')) if (fileName.includes('.')) { fileName = fileName.substring(0, fileName.lastIndexOf('.')) + suffix } else { fileName = fileName + suffix } return fileName}/*** Upload.vue**/import { UploadProps } from 'ant-design-vue/es/upload'import { fixFileSuffix, getDefaultProps } from '../../_utils'export default { name: 'BUpload', props: Object.assign({}, UploadProps, { uploadBusinessType: { // 上传文件的业务类型 type: String, default: '', required: true }, onlyOne: { type: Boolean, default: false } }), data () { return { uploadData: {}, uploadUrl: '' } }, methods: { async customBeforeUpload (file, fileList) { // 执行OSS参数获取 const query = { uploadBusiType: this.uploadBusinessType, uploadFileName: this.fileName ? fixFileSuffix(this.fileName, file.name) : file.name, timestamp: new Date().getTime() } try { const { data } = await this.$http.system.getUploadOssFileParams(query) const { url, ossAccessKeyId, key } = data this.uploadUrl = url this.uploadData = { ...data, 'Content-type': file.type, OSSAccessKeyId: ossAccessKeyId } this.$nextTick(() => { // 执行外层函数回调 const { beforeUpload } = this if (beforeUpload) { // 插入OSS返回的相对路径 const index = fileList.findIndex(it => it.uid === file.uid) file.relativePath = key fileList.splice(index, 1, file) beforeUpload(file, fileList) } }) } catch (e) { console.error('获取oss上传参数失败!', e) // eslint-disable-next-line prefer-promise-reject-errors return Promise.reject(false) } }, handleChange (info) { if (this.onlyOne && info.fileList.length >= 2) { // 如果只能传一个且长度大于2的时候执行 let fileList = [...info.fileList] fileList = fileList.slice(-1) // 只支持上传1个 fileList = fileList.map(file => { if (file.response) { file.url = file.response.url } return file }) info.file = fileList[0] info.fileList = fileList } // if (info.file.status !== 'uploading') { // console.log(info.file, info.fileList, info.event) // } if (info.file.status === 'done') { this.$message.success(`${info.file.name}上传成功`) } else if (info.file.status === 'error') { this.$message.error(`${info.file.name} 上传失败 : (`) } const { change } = this.$listeners change && change(info) }, fillUploadParams (props) { props.action = this.uploadUrl props.data = this.uploadData } }, render () { const props = getDefaultProps(UploadProps, this) // 插入beforeUpload props.beforeUpload = this.customBeforeUpload // 补充上传参数 this.fillUploadParams(props) return ( <a-upload {...{ props }} {...{ on: { change: this.handleChange } }} > { Object.keys(this.$slots).map(name => { return this.$slots[name] }) } </a-upload> ) }} Post author: 落小梅 | 月小芽Post link: https://yuexiaoya.com/2020/06/22/frontend/vue/ant-design/upload%E4%B8%8A%E4%BC%A0%E8%87%B3oss%E7%BB%84%E4%BB%B6/Copyright Notice: All articles in this blog are licensed under unless otherwise stated.