Skip to content

base64 转 流文件 #25

Description

@JslinSir

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)
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions