Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64 转 流文件 #25

Open
JslinSir opened this issue Oct 10, 2022 · 0 comments
Open

base64 转 流文件 #25

JslinSir opened this issue Oct 10, 2022 · 0 comments

Comments

@JslinSir
Copy link
Owner

base64转文件流

/**
 * Base64编码转换成blob, (data:image/png;base64,xxxx)
 * @param {*} base64
 * @return {*}
 */
 export function base64ToBlob(base64) {
  const parts = base64.split(';base64,')
  const contentType = parts[0].split(':')[1] // image/png
  const raw = atob(parts[1]) // base64数据
  const rawLength = raw.length
  const uInt8Array = new Uint8Array(rawLength)
  for (let i = 0; i < rawLength; ++i) {
    uInt8Array[i] = raw.charCodeAt(i)
  }
  const blobFile = new Blob([uInt8Array], {
    type: contentType,
  }) 
  return new File([blobFile],`${new Date().getTime()}.${contentType.replace(/image\//g,'')}`,{
    type: contentType,
  })
}

文件流上传

export function uploadImage(file) {
 const formData = new FormData()
  formData.append('file', file)
  formData.append('xxx', 'xxx') //接口入参
 
 
  return request('xxxxx', formData)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant