0
点赞
收藏
分享

微信扫一扫

base64 转文件上传

zhaoxj0217 2022-08-29 阅读 67

// 将base64转换为blob
dataURLtoBlob (dataurl) {
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let bstr = atob(arr[1]) // .replace(/%0A|\s/g, '')
console.info('-----------123---------')
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: mime })
},
// 将blob转换为file
blobToFile (theBlob, fileName) {
theBlob.lastModifiedDate = new Date()
theBlob.name = fileName
return theBlob
},
getBase64Mock () {
return {
'base64': 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAK8AAAA2CAYAAACx+8n+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJVSURBVHhe7ZjBauMwEED3q/fDetp/yL2nHnNYH3xpwNBAUD2yHcv2xFbDoGjKe/AoJJIbw+tE9Z8A4BTiBbcQL7iFeMEtxAtuIV5wC/GCW4gX3EK84BbiBbcQL7iFeMEtxAtuIV5wC/GCW4gX3FJVvH///cdfrDXEi8W0hnixmNYQLxbTGuLFYlpDvFhMa4gXi2kN8WIxrSFeLKY1xJvj+1fowjWctPdWni4hNOfkNdl7/QpvyZpJWZtL134me7vQaJ/nfA3h0i1fq0hriDfH7HinqOTnDmPMy9CHPYvwJ/soiXcL8eaYG+8mns/wcX28b473cewxWuJVId4cs+KVUFdf77JvJ6YYbyvXHknWvrW3+4QmXh3izTHGq3ELH+/jGgmnp2u7GPERMnE35+PxGsIiVjXeHJLPV4HWEG+Oh5NXYrqF5nJbRHa67E/rGG+/5s568kb6ANvMyVu51hBvjkfx9u83fVwS3BzZ4+k4rVlO3uHYIWz+acs9NlSuNcSb4+HkHVzGq7i6zubYEJ0iTn7fj44NdR0VUq0h3hyfiVfOr+nz3XiNZVj3eON7M4eTV9arz44lfOJ9CdoNV+Ez8YoScE9zHiblOkp98iZT9cHTBtmnP1Ug3peh3XAVPhvvaIxN2T+8nsf9usoEnyXel6HdcHFXX+HHzLHsnnmVPwB98iomk3d/D/G+DO2G8fdoDfFiMa0hXiymNcSLxbSGeLGY1hAvFtMa4sViWkO8WExriBeLaU1V8QL8BOIFtxAvuIV4wS3EC24hXnAL8YJbiBecEsI3JvIyp/Bd34cAAAAASUVORK5CYII=',
'key': '202172395245566',
'fileName': '66' + Math.random() + '.jpg'
}
},
base64UpFile (ctx, next) {
let base64Arr = [this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock(),
this.getBase64Mock()
]
ctx.base64Arr = base64Arr
ctx.index = 0

base64Arr.forEach(item => {
let formData = new FormData()
let blob = this.dataURLtoBlob(item.base64)
let file = this.blobToFile(blob, item.fileName)
formData.append('file', file, item.fileName)

this.$Spin2.show('图片上传中')
axios.request({
method: 'post',
url: '/processFile/fileUpload',
data: formData
}).then(res => {
return res.data
}).then(res => {
// console.info('axios res', res)

const params = {
reportingId: this.params.reportingId,
typeCode: this.typeCode1,
name: item.fileName,
url: res.data.split('-')[0]
}
this.$api('/process-api/file/saveOne', params).then(res => {
this.$Spin2.hide()
if (res && res.status === 20) {
// this.$Message.success('上传成功')
ctx.index = ctx.index + 1
next()
}
})
})
})
},
flushTree (ctx, next) {
if (ctx.index === ctx.base64Arr.length) {
this.$Message.success('上传成功')
this.$refs.part1.init()
}
},
base64ToFileHandle () {
const ac = this.$getAc()
ac.use(this.base64UpFile)
ac.use(this.flushTree)
ac.run()
},

---------------------------------------------
生活的意义并不是与他人争高下,而在于享受努力实现目标的过程,结果是对自己行动的嘉奖。
↑面的话,越看越不痛快,应该这么说:

生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!

新博客 ​​​https://www.VuejsDev.com​​ 用于梳理知识点



举报

相关推荐

0 条评论