js上传文件流file转base64,base64转blob,blob转url访问地址

/**
 * 上传附件转base64
 * @param {File} file 文件流
 */
export const fileByBase64 = (file, callback) => {
  let reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function (e) {
    console.log(e.target.result);
    callback(e.target.result)
  };
}
/**
 * base64转Blob
 * @param {*} data 
 */
export const base64ByBlob = (base64, callback) => {
  let arr = base64.split(','), mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  console.log(new Blob([u8arr], { type: mime }))
  callback(new Blob([u8arr], { type: mime }))
}
/**
 * Blob转url
 */
let url = window.URL.createObjectURL(blob)

你可能感兴趣的:(js上传文件流file转base64,base64转blob,blob转url访问地址)