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

前端 未结 4 1779
花落未央
花落未央 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:22

    If anyone cares: here is the coffescript version of the function given above!

      convertToBlob = (base64) ->
        binary = atob base64.split(',')[1]
        array = []
        for i in [0...binary.length]
          array.push binary.charCodeAt i
        new Blob [new Uint8Array array], {type: 'image/jpeg'}
    
    0 讨论(0)
  • 2021-01-30 19:24

    Not sure if OP has already solved this, but I'm working on a very similar feature. In doing a little research, I came across these articles that might be helpful.

    • http://blog.danguer.com/2011/10/25/upload-s3-files-directly-with-ajax/
    • http://www.tweetegy.com/2012/01/save-an-image-file-directly-to-s3-from-a-web-browser-using-html5-and-backbone-js/
    0 讨论(0)
  • 2021-01-30 19:27

    I have found a way to do this. After a lot of searching a looking at different tutorials.

    You have to convert the Data URI to a blob and then upload that file to S3 using CORS, if you are working with multiple files I have separate XHR requests for each.

    I found this function which turns your the Data URI into a blob which can then be uploaded to S3 directly using CORS (Convert Data URI to Blob )

    function dataURItoBlob(dataURI) {
        var binary = atob(dataURI.split(',')[1]);
        var array = [];
        for(var i = 0; i < binary.length; i++) {
            array.push(binary.charCodeAt(i));
        }
        return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
    }
    

    Here is a great tutorial on uploading directly to S3, you will need to customise the code to allow for the blob instead of files.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题