本文是自己平时业务需求中的总结,如果你需要借鉴本文,那么你的上传行为应该是: 项目中下载ali-oss 依赖,我这里使用的是^6.0.1 版本 使用阿里云oss,将图片上传到阿里云,然后阿里云将图片地址返回,我们拿到图片地址,将它传递给后端
本文主要使用this.$refs.upload.uploadFiles ,获取到,el-upload 上传组件中的默认上传列表,无论上传失败还是成功,都会将图片保存在这里,所以我们只需要对他进行操作,就能实现我们想要的功能
1. 初始化阿里云对象,并获取阿里云token,用于上传图片
这里我把初始化阿里云的过程,写在一个js文件中,因为用到的地方比较多,如果你使用的次数比较少,你也可以直接写在页面中 (闲的没事还写了个随机生成文件名的函数,用于保证上传的每个文件有一个独一无二的文件名)
import { get } from '@/utils/request';
var moment = require('moment');
var client = '123';
export function uploadOSS() {
return get('这里应该是一个后端的url,用于获取阿里云token').then((res) => {
if (res.data.result === '01') {
client = new OSS.Wrapper({
secure: true,
region: 'oss-cn-hangzhou',
bucket: 'wuneng-public',
accessKeyId: res.data.data.keyid,
accessKeySecret: res.data.data.keysecret,
stsToken: res.data.data.token,
});
return client;
}
});
}
export function randomFileName() {
var result = [];
for (var i = 0; i < 6; i++) {
var ranNum = Math.ceil(Math.random() * 25);
result.push(String.fromCharCode(65 + ranNum));
}
const dateStr = moment().format('YYYYMMDDhhmmss');
const randomStr = result.join('');
return randomStr + dateStr;
}
2. 组件编写、
这是一个完整的组件,可以直接拿来用的,注释写的也很清楚 你的项目中需要有:axios依赖,阿里云依赖,element-ui依赖,moment.js依赖
<template>
<div>
<el-dialog :visible.sync="innerVisible" :show-close="false" append-to-body>
<img width="100%" :src="dialogImageUrl" alt="" />
</el-dialog>
<el-upload ref="upload" :http-request="handleUpload" action="" list-type="picture-card" accept="image/*" :limit="3">
<i slot="default" class="el-icon-plus"></i>
<div slot="file" slot-scope="{ file }">
<img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
<span class="el-upload-list__item-actions">
<span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
<i class="el-icon-zoom-in"></i>
</span>
<span class="el-upload-list__item-delete" @click="handleDownload(file)">
<i class="el-icon-download"></i>
</span>
<span class="el-upload-list__item-delete" @click="handleRemove(file)">
<i class="el-icon-delete"></i>
</span>
</span>
</div>
</el-upload>
</div>
</template>
<script>
import { uploadOSS, randomFileName } from '@/utils/uploadOSS.js';
import axios from 'axios';
export default {
data() {
return {
dialogImageUrl: '',
innerVisible: false,
fileList: [],
};
},
mounted() {
this.handleUploadOSS();
},
methods: {
handleUploadOSS() {
uploadOSS().then((res) => {
this.client = res;
});
},
async handleUpload(file) {
const result = await this.client.multipartUpload(randomFileName() + file.file.name, file.file);
if (result.res.status != 200) {
this.$message.error('上传失败!');
return;
}
this.$message.success('上传成功!');
console.log(result.res.requestUrls[0]);
var tempUrl = '';
if (result.res.requestUrls[0].indexOf('?uploadId=') != -1) {
tempUrl = result.res.requestUrls[0].split('?uploadId=')[0];
} else {
tempUrl = result.res.requestUrls[0];
}
const uid = this.$refs.upload.uploadFiles.slice(-1)[0].uid;
this.fileList.push({ tempUrl, uid });
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.innerVisible = true;
},
handleRemove(file) {
var number = '';
this.$refs.upload.uploadFiles.forEach((v, i) => {
v.uid == file.uid ? (number = i) : '';
});
this.$refs.upload.uploadFiles.splice(number, 1);
this.fileList.splice(number, 1);
},
handleDownload(file) {
var number = '';
this.$refs.upload.uploadFiles.forEach((v, i) => {
v.uid == file.uid ? (number = i) : '';
});
const tempUrl = this.fileList.slice(number, 1)[0].tempUrl;
axios.get(tempUrl + '?time=' + Date.now(), { responseType: 'blob' }).then((res) => {
const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = downloadUrl;
link.download = 'xxx.png';
document.body.appendChild(link);
link.click();
link.remove();
});
},
},
};
</script>
|