How to upload base64 encoded image data to S3 using JavaScript only?

前端 未结 4 1785
花落未央
花落未央 2021-01-30 18:43

I have a rails app on Heroku (cedar env). It has a page where I render the canvas data into an image using toDataURL() method. I\'m trying to upload the returned ba

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 19:27

    Jamcoope's answer is very good, however the blob constructor is not supported by all browsers. Most notably android 4.1 and android 4.3. There are Blob polyfills, but xhr.send(...) will not work with the polyfill. The best bet is something like this:

    var u = dataURI.split(',')[1],
        binary = atob(u),
        array = [];
    
    for (var i = 0; i < binary.length; i++) {
        array.push(binary.charCodeAt(i));
    }
    
    var typedArray = Uint8Array(array);
    
    // now typedArray.buffer can be passed to xhr.send
    

提交回复
热议问题