最近在vue开发中遇到了一些问题(对图片进行裁剪的插件croppa在封装blob时遇到的坑):html
1.引入croppa:vue
import Vue from 'vue'app
import Croppa from 'vue-croppa'this
2.html片断spa
<croppa v-model="tempData.croppa" ref="supervisionUploader" 插件
:width="600"orm
:height="300"htm
placeholder-color="#000"对象
:remove-button-size="25"接口
placeholder="请选择一张图片"
:placeholder-font-size="24">
</croppa>
3.这是遇到坑的地方
this.formData = new FormData()
var that = this
//获取croppa的blob对象
this.croppa.generateBlob((blob) => {
let file = that.croppa.getChosenFile()
let croppaFile = new File([blob], file.name) //将blob对象转换成文件
that.formData.append('croppedImage', croppaFile) //封装到formdata中
addBanner(that.formData).then(response => { //调用接口
if (response.status === 200 && response.data.result !== 'error') {
that.getList()
that.$notify({
title: '成功',
message: '添加成功',
type: 'success',
duration: 2000
})
} else {
that.$notify({
title: '失败',
message: '添加失败',
type: 'error',
duration: 2000
})
}
})
})