upload canvas data to s3

前端 未结 5 1559
刺人心
刺人心 2020-12-25 13:53

Now since the amazon has enabled CORS I was wondering if this is possible.

Can the html canvas data (on client browser) be converted to a <

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 14:43

    Here is a working example where you take a data URL from a canvas and upload it to S3:

    var dataUrl = canvas.toDataURL("image/jpeg");
    var blobData = dataURItoBlob(dataUrl);
    var params = {Key: "file_name", ContentType: "image/jpeg", Body: blobData};
    bucket.upload(params, function (err, data) {});
    

    dataURItoBlob:

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

提交回复
热议问题