Convert Data URI to File then append to FormData

后端 未结 14 2628
逝去的感伤
逝去的感伤 2020-11-21 07:14

I\'ve been trying to re-implement an HTML5 image uploader like the one on the Mozilla Hacks site, but that works with WebKit browsers. Part of the task is to extract an imag

14条回答
  •  长发绾君心
    2020-11-21 07:38

    My preferred way is canvas.toBlob()

    But anyhow here is yet another way to convert base64 to a blob using fetch ^^,

    var url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
    
    fetch(url)
    .then(res => res.blob())
    .then(blob => {
      var fd = new FormData()
      fd.append('image', blob, 'filename')
      
      console.log(blob)
    
      // Upload
      // fetch('upload', {method: 'POST', body: fd})
    })

提交回复
热议问题