HappyJayXin
12/30/2019 - 1:58 PM

Transform image format from dataUrl to File.

Transform image format from dataUrl to File.

/**
 * 轉換圖片 dataURL 為 File
 * @param dataurl 圖片dataurl
 * @param filename 檔案名稱
 */
export default function(dataurl, filename) {
  const arr = dataurl.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const bstr = arr[1];
  let n = bstr.length;
  const u8arr = new Uint8Array(n);

  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }

  return new File([u8arr], filename, { type: mime });
}