VueElementUI+OSS实现上传⽂件功能
Element提供了upload上传组件,可以查看官⽹组件的详细介绍;查看upload组件的上传源码,使⽤的是XHR对象上传⽂件。在项⽬实践中,发现该上传⽅法上传⼤⽂件时会出现问题,所以决定使⽤阿⾥云对象存储服务(Object Storage Service,简称OSS),详细介绍可以查看官⽹,本⽂主要讲解的是OSS上传⽂件的前端,后台开发请查看官⽹介绍,讲解了前端如何进⾏上传。
1、使⽤npm安装SDK的开发包:
npm install ali-oss
2、OSS配置
let OSS = require('ali-oss');
let client= new OSS.Wrapper({
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name',
region: 'oss-cn-hangzhou'
});
最新版的没有了Wrapper构造函数,直接配置
let oss = require('ali-oss');
var client = oss({
accessKeyId: 'your access key',
accessKeySecret: 'your access secret',
bucket: 'your bucket name',
region: 'oss-cn-hangzhou'
});
3、上传⽂件
OSS上传⽂件分为上传Buffer内容、上传blob数据、分⽚上传,此处展⽰
let result = await client.multipartUpload('object-key', 'local-file', {
progress: async function (p) {
console.log('Progress: ' + p);
}
});
js调⽤OSS上传⽂件已经没有问题,但是我们需要实现公⽤上传组件,Element UI已经实现了Upload上传组件,包括上传进度、异常处理、拖拽等;那我们还有必要重新开发⼀个像Upload⼀样的组件吗?通过查看Upload组件的API发现有⼀个http-request属性,http-request覆盖默认的上传⾏为,可以⾃定义上传的实现;看到这个我就省了很多事,直接⽤OSS上传覆盖XHR上传。代码如下:
<template>
<div>
<el-upload
:auto-upload="false"前端大文件上传解决方案
:action="uploadUrl"
ref="upload"
:before-upload="fnBeforeUpload"
:on-success="fnUploadSuccess"
:on-exceed="fnUploadExceed"
:data="data_extra"
:headers="uploadHeaders"
:http-request="fnUploadRequest"
drag
:limit="files"
:disabled="disabled"
multiple>
<i class="el-icon-upload"></i>
<div class="el-upload__text">将⽂件拖到此处,或<em>点击上传</em></div>
<div slot="tip" class="el-upload__tip">上传⽂件⼤⼩不能超过 1G</div>
</el-upload>
</div>
</template>
<script>
import UploadFileApi from '@/api/system/UploadFileApi'
import * as util from 'utils/common'
import OSS from 'ali-oss'
export default {
name: 'fileUpload',
props: {
data_extra: {
type: Object,
required: true
},
accept: {
type: Array,
required: true
}
},
data () {
return {
uploadUrl: '',
uploadFileLength: 0,
uploadFileSuccess: 0,
uploadFileNames: [],
uploadFileName: [],
fileList: [],
files: 10,
uploadHeaders: {
authorization: ''
},
disabled: false
}
},
methods: {
/**
* @description [fnUploadRequest 覆盖默认的上传⾏为,实现⾃定义上传] * @author shanshuizinong
* @param {object} option [上传选项]
* @return {null} [没有返回]
*/
async fnUploadRequest (option) {
try {
let vm = this
vm.disabled = true
// 获取OSS配置信息
let uploadFileApi = new UploadFileApi()
let ret = await uploadFileApi.fileOssParams()
if (!(ret.data && de === '0' && ret.data.data)) {
throw new Error('获取OSS参数失败')
}
let ossData = JSON.parse(ret.data.data)
let relativePath = lativePath
let client = new OSS.Wrapper({
policy: ossData.policy,
accessKeyId: ossData.accessid,
accessKeySecret: ossData.accesssecret,
bucket: ossData.bucket,
signature: ossData.signature
})
let file = option.file
const point = file.name.lastIndexOf('.')
let suffix = file.name.substr(point)
let fileName = file.name.substr(0, point)
let date = vm.$moment().format('YYYYMMDDHHmm')
let fileNames = `${fileName}_${date}${suffix}`
// 分⽚上传⽂件
ret = await client.multipartUpload(relativePath + fileNames, file, {
progress: async function (p) {
let e = {}
e.percent = p * 100
}
})
console.log(ret)
if (s.statusCode === 200) {
// Success(ret)
return ret
} else {
vm.disabled = false
}
} catch (error) {
<(error)
this.disabled = false
this.$ssage)
}
},
/**
* @description [fnUploadSuccess ⽂件上传成功的函数]
* @author shanshuizinong
* @return {null} [没有返回]
*/
async fnUploadSuccess () {
// TODO
}
}
}
</script>
查看upload源码知道httpRequest会有⼀个⽂件上传的option选项作为参数,源码中上传进度、上传成功、上传失败都是通过option调⽤其⽅法进⾏触发。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。