vue+ant desgin pro 上传图片至七牛云
1. 在public文件目录的index.html中引入
<script src="http://mryt.kangdaedu.com/es6-promise.min.js"></script>
<script src="http://mryt.kangdaedu.com/aliyun-oss-sdk-5.3.1.min.js"></script>
<script src="http://mryt.kangdaedu.com/aliyun-upload-sdk-1.5.0.min.js"></script>
2. 在main.js文件中添加
import UUID from "vue-uuid"
Vue.use(UUID)
3. 在page页面中引入
import * as qiniu from "qiniu-js";
4. 在项目命令窗口安装
1、qiniu-js
2、vue-uuid
5. 后端接口返回数据参数,如下图所示:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c89d9474ce572a0ec9c14038f2627385.png)
6. 前端编辑代码如下:
<!--点击按钮-->
<a-button @click="showModal">
<a-icon type="upload" />上传logo
</a-button>
<!-- 上传图片弹框 -->
<a-modal title="上传图片" v-model="isUploadingImg">
<!-- 图片上传选择按钮 -->
<p>
<a href="#" class="file">
<input
type="file"
@change="selectImg($event,'uploadImg')"
accept=".jpg, .png, .gif"
size="400"
/>上传图片
</a>
</p>
<!-- 进度条 -->
<a-progress :percent="UploadPhotoPercent" size="small" status="active" v-if="isShowProgress" />
</a-modal>
<!-- 定义参数-->
imgUuid: "",
isShowProgress: false,
previewImage: "",
UploadPhotoPercent: 0,
isUploadingImg: false,
isEditImg: false,
editPhotoPercent: 0,
isShowEditProgress: false,
selectImg(event, type) {
var that = this;
if (type == "uploadImg") {
that.isUploadingImg = true;
that.isShowProgress = true;
} else if (type == "editImg") {
that.isEditImg = true;
that.isShowEditProgress = true;
}
if (event.target.files.length > 0) {
var file = event.target.files[0];
var imgName = file.name;
var idx = imgName.lastIndexOf(".");
if (idx != -1) {
var ext = imgName.substr(idx + 1);
ext = ext.toLowerCase();
if (ext != "jpg" && ext != "png" && ext != "gif") {
that.$message.error(
"请选择正确的图片格式上传,图片为jpg、png格式!"
);
} else {
that.uploadImgToQiniu(file, type);
}
} else {
that.$message.error("图片格式为jpg、png格式,在上传!");
}
}
},
uploadImgToQiniu(file, type) {
var that = this;
this.$api.getUploadToken().then(res => {
var uptoken = res.data.data.uploadToken;
var key = that.$uuid.v1();
let config = {
useCdnDomain: true,
region: qiniu.region.z1
};
let putExtra = {
fname: file.name,
params: {},
mimeType: ["image/jpeg", "image/png", "image/gif"]
};
var observable = qiniu.upload(file, key, uptoken, putExtra, config);
observable.subscribe({
next: result => {
if (type == "uploadImg") {
that.isShowProgress = true;
that.UploadPhotoPercent = Math.ceil(result.total.percent * 100);
} else if (type == "editImg") {
that.isShowProgress = true;
that.UploadPhotoPercent = Math.ceil(result.total.percent * 100);
}
},
error: errResult => {
that.$message.error("上传失败,失败原因:" + errResult);
if (type == "uploadImg") {
that.isShowProgress = false;
} else if (type == "editImg") {
that.isShowEditProgress = false;
}
},
complete: result => {
if (type == "uploadImg") {
that.previewImage = "http://file.jsb.kangdaedu.com/" + key;
that.imgUuid = key;
that.isUploadingImg = false;
that.isShowProgress = false;
that.$message.success("上传成功");
} else if (type == "editImg") {
that.imgurl = "http://file.jsb.kangdaedu.com/" + key;
that.logo = key;
that.isEditImg = false;
that.isShowEditProgress = false;
that.$message.success("上传成功");
}
}
});
});
},