阿里云oss(multipartUpload拆成3个接口版本)

alioss
yujialong 1 year ago
parent cce6029403
commit 63dccce56b
  1. 155
      src/pages/match/details/index.vue

@ -648,13 +648,13 @@
<el-upload :before-upload="beforeUpload"
:on-remove="handleRemove"
:on-error="uploadError"
:on-success="uploadSuccess"
:before-remove="beforeRemove"
:limit="1"
action=""
:on-success="uploadSuccess"
:on-exceed="handleExceed"
:action="api.fileUploadNakadai"
:headers="headers"
:file-list="fileList"
:http-request="handleRequest"
name="file">
<el-button size="small"
type="primary"
@ -662,9 +662,9 @@
<div slot="tip"
class="el-upload__tip">
<!-- <el-progress v-if="showProgress"
<el-progress v-if="showProgress"
:stroke-width="3"
:percentage="progressPercent"></el-progress> -->
:percentage="uploadProgress"></el-progress>
<p>请上传大小1G以内的文件支持常见文件格式</p>
<p>只允许上传一个文件如有多个文件则需打包再上传</p>
</div>
@ -809,6 +809,17 @@ export default {
curFileId: '',
showProgress: false,
progressPercent: 0,
client: null,
chunkSize: 5 * 1024 * 1024,
chunkArr: [],
showProgress: false,
uploadProgress: 0,
uploadId: '',
fileName: '',
size: 0,
};
},
computed: {
@ -832,6 +843,8 @@ export default {
this.getProgress()
this.getNotice()
this.getTeam()
this.initOss()
},
methods: {
...mapMutations('match', [
@ -1428,18 +1441,103 @@ export default {
}
},
initOss () {
this.client = new OSS({
region: 'oss-cn-shenzhen',
accessKeyId: 'LTAI4FzqQHnk4rozqLZ8jCNj',
accessKeySecret: 'mveW7B1OyFoKUkHm8WsxmrjHmkJWHq',
bucket: 'huoran'
})
},
//
beforeUpload (file) {
const oversize = file.size / 1024 / 1024 < 1000
if (!oversize) util.warningMsg('请上传小于1GB的附件!')
if (oversize) {
this.showProgress = true
// this.uploadFile(file)
return true
} else {
return false
}
},
progress1 (p, _checkpoint) {
// Object
console.log(p);
//
console.log(_checkpoint);
},
async uploadFile (file) {
const result = await this.client.multipartUpload(file.name, file, {
progress: this.progress1
});
console.log(33, result);
return
// this.chunkArr = [];
// this.showProgress = true
// this.uploadProgress = 0;
// //
// this.chunks = this.sliceFile(file);
// const { name: fileName, type: mimeType } = file;
// this.fileName = fileName;
// //Upload ID
// const result = await this.client.initMultipartUpload(fileName);
// this.uploadId = result.uploadId;
// //
// this.uploadChunk(this.chunks[0], 1);
},
sliceFile (file) {
const fileSize = file.size;
this.size = file.size;
const chunks = Math.ceil(fileSize / this.chunkSize); //
const allChunks = [];
for (let i = 0; i < chunks; i++) {
const start = i * this.chunkSize;
const end = Math.min(start + this.chunkSize, fileSize);
const chunk = file.slice(start, end);
allChunks.push(chunk);
}
return allChunks;
},
async uploadChunk (chunk, partNumber) {
//
const part = await this.client.uploadPart(
this.fileName,
this.uploadId,
partNumber,
chunk,
0,
chunk.size
);
this.chunkArr.push({ number: partNumber, etag: part.etag });
//
this.uploadProgress = Number(
((partNumber / this.chunks.length) * 100).toFixed(2)
);
if (partNumber < this.chunks.length) {
//
partNumber++;
this.uploadChunk(this.chunks[partNumber - 1], partNumber);
} else {
//
const res = await this.client.completeMultipartUpload(
this.fileName,
this.uploadId,
this.chunkArr
);
this.showProgress = false
console.log("🚀 ~ file: index.vue:1513 ~ uploadChunk ~ res:", 'http://huoran.oss-cn-shenzhen.aliyuncs.com/' + res.name)
}
},
//
async handleRequest ({ file }) {
this.uploadFile(file)
},
uploadError (err, file, fileList) {
this.$message({
message: "上传出错,请重试!",
@ -1461,6 +1559,8 @@ export default {
}).catch(res => { })
},
uploadSuccess (res) {
console.log("🚀 ~ file: index.vue:1541 ~ uploadSuccess ~ res:", res)
return
const result = res.filesResult
//
this.handleRemove()
@ -1482,44 +1582,6 @@ export default {
util.successMsg(`上传成功!`);
}).catch(res => { })
},
progress (p, _checkpoint) {
// Object
console.log(p);
//
console.log(_checkpoint);
},
//
async handleRequest (options) {
const { file } = options
const headers = {
// Object
'x-oss-storage-class': 'Standard',
// Object访
// 'x-oss-object-acl': 'private',
// URL访example.txt
// 'Content-Disposition': 'attachment; filename="example.txt"',
// Object
'x-oss-tagging': 'Tag1=1&Tag2=2',
// PutObjectObjecttrueObject
'x-oss-forbid-overwrite': 'true',
};
const client = new OSS({
region: 'oss-cn-shenzhen',
accessKeyId: 'LTAI4FzqQHnk4rozqLZ8jCNj',
accessKeySecret: 'mveW7B1OyFoKUkHm8WsxmrjHmkJWHq',
bucket: 'huoran'
})
const res = await client.multipartUpload(file.name, file, {
progress: this.progress,
// headers,
// metaObjectheadObjectmeta
meta: {
year: 2020,
people: 'test',
}
})
console.log("🚀 ~ file: index.vue:1505 ~ handleRequest ~ res:", res)
},
//
stageSubmit () {
if (this.submiting) return false
@ -2081,4 +2143,7 @@ export default {
/deep/.el-upload__tip {
color: #727272;
}
/deep/.el-progress {
white-space: nowrap;
}
</style>
Loading…
Cancel
Save