Last active
January 10, 2018 09:11
-
-
Save Mark24Code/27c88b3e04117e6c6cf6507828378fae to your computer and use it in GitHub Desktop.
base64转文件,axios上传文件(适用于裁剪头像上传)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//base64转 bob文件 | |
function dataURItoBlob(dataURI) { | |
// convert base64/URLEncoded data component to raw binary data held in a string | |
var byteString; | |
if (dataURI.split(',')[0].indexOf('base64') >= 0) | |
byteString = atob(dataURI.split(',')[1]); | |
else | |
byteString = unescape(dataURI.split(',')[1]); | |
// separate out the mime component | |
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]; | |
// write the bytes of the string to a typed array | |
var ia = new Uint8Array(byteString.length); | |
for (var i = 0; i < byteString.length; i++) { | |
ia[i] = byteString.charCodeAt(i); | |
} | |
return new Blob([ia], {type:mimeString}); | |
} | |
//axios发送 file | |
//avatar_file.type==> 'image/png' | |
let avatar_file = dataURItoBlob(res); | |
let avatar_filename = 'avatar'+'.'+avatar_file.type.split('/')[1]; | |
let formdata = new FormData(); | |
formdata.append('file',avatar_file,avatar_filename) | |
axios.request({ | |
url: UPLOAD_AVATAR, | |
method: 'post', | |
headers: { | |
'version': 'v2.0', | |
'access-token': self.user_token, | |
'Content-Type': 'multipart/form-data' | |
}, | |
data:formdata | |
}) | |
.then((resp) => { | |
let res = resp.data; | |
if (res.code == 1) { | |
self.$toast.center('头像上传成功'); | |
self.photo = res.data.fileUrl; | |
} else { | |
self.$toast.center(res.msg); | |
} | |
}) | |
.catch((err) => { | |
self.$toast.center(err); | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment