0
点赞
收藏
分享

微信扫一扫

217-jquery,将base64的图片,转为file文件,并通过$.ajax上传

夏沐沐 2023-11-17 阅读 22
var base64 = '';
const blob = dataURLToBlob(base64);
  const file = blobToFile(blob, 'image.jpg');

  uploadImage(file);
  
  
function dataURLToBlob(base64) {
  const parts = dataURL.split(';base64,');
  const contentType = parts[0].split(':')[1];
  const byteCharacters = atob(parts[1]);
  const byteArrays = [];

  for (let i = 0; i < byteCharacters.length; i++) {
    byteArrays.push(byteCharacters.charCodeAt(i));
  }

  return new Blob([new Uint8Array(byteArrays)], { type: contentType });
}

function blobToFile(blob, fileName) {
  return new File([blob], fileName, { type: blob.type });
}
举报

相关推荐

0 条评论