js 中新增一个方法
function b64toBlob(b64Data, contentType='', sliceSize=512) {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
base64图片数据
// dataURL to blob
// 假设一个dataURL
const ImageURL = "data:image/jpeg;base64,adfasdfsafsdf
// Split the base64 string in data and contentType
const block = ImageURL.split(";");
// Get the content type of the image
const contentType = block[0].split(":")[1];// In this case "image/jpeg"
// get the real base64 content of the file
const realData = block[1].split(",")[1];// In this case "R0lGODlhPQBEAPeoAJosM...."
// Convert it to a blob to upload
var blob = b64toBlob(realData, contentType);
创建formData对象
// new a formData
const formData = new FormData();
formData.append('blob', blob);
Ajax提交
// upload
$.ajax({
url:url,
data: formData
type:"POST",
contentType:false,
processData:false,
error:function(err){
},
success:function(data){
},
});
后台可打印$_FILES 查看
上一篇: PHP 常用函数...
下一篇: PHP 文件写入数组...