上传base64镜像数据到Amazon S3



好的,在这个phonegap应用程序中,我从设备相机的一个大图像文件开始。
然后我降低分辨率,允许用户使用javascript画布和一些非常酷的代码darkroomJS来裁剪它。最后,darkroomJS使用toDataURL()方法从画布中获取base64数据。

现在我需要把它上传到Amazon S3。

我可以用表单把文件从我的电脑上传到S3。我正在做的是发送base64数据。

我还有一个将base64数据转换为blob的函数。(我已经读过了,但我愿意接受其他想法)。

 <form action="https://mybucketname.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
      <input type="" id="key" name="key" value="uploads/${filename}">
      <input type="" id="AWSAccessKeyId" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY"> 
      <input type="" id="acl" name="acl" value="private"> 
      <!-- <input type="" id="success_action_redirect" name="success_action_redirect" value="http://localhost/"> -->
      <input type="" id="policy" name="policy" value="YOUR_POLICY_DOCUMENT_BASE64_ENCODED">
      <input type="" id="signature" name="signature" value="YOUR_CALCULATED_SIGNATURE">
      <input type="" id="Content-Type" name="Content-Type" value="image/jpeg">
<!--       <input id="file" name="file" >  -->
      <br> 
      <input type="submit" value="Upload File to S3"> 
    </form> 

从服务器获得凭据后,我使用jquery编写凭据。那部分很好。

问题是我如何把一个blob的形式?

我试过两种方法。使用jQuery设置。

page.find美元("#文件")。attr("价值",blobData);//将字符串[object Blob]文本插入到字段中并将其发送到S3,不是很有用

also I've try:

        var fd = new FormData(document.forms[0]);
        fd.append('file', blobData);

//无效;S3然后抱怨它正在等待一个文件,但是没有得到。

我做错了什么?我该怎么做呢?

好了,有办法了:

我想让表单实际呈现在页面上有点迫使浏览器对blob进行字符串化。
功能s3Man。getS3Policy只是向我的服务器发出一个AJAX请求,以获取签名的s3策略、密钥等。

this.uploadBase64ImgToS3 = function(base64Data, filename, callback){
    var xmlhttp = new XMLHttpRequest();    
    var blobData = dataURItoBlob(base64Data);
    var contentType = false;
    if (filename.match(/.png/)) var contentType = 'image/png';
    if (filename.match(/.jpg/)) contentType = 'image/jpeg';
    if (!contentType){
        xStat.rec("content type not determined, use suffix .png or .jpg");
        return;
    }
    s3Man.getS3Policy(filename, function(s3Pkg){
        console.log("policy:", s3Pkg);
        var fd = new FormData();    
        fd.append('key', s3Pkg.filename);    
        fd.append('acl', 'public-read');    
        fd.append('Content-Type', contentType);    
        fd.append('AWSAccessKeyId', s3Pkg.awsKey);    
        fd.append('policy',  s3Pkg.policy);
        fd.append('signature', s3Pkg.signature);    
        fd.append("file", blobData);
        xmlhttp.open('POST', 'https://swoopuploadspublic.s3.amazonaws.com/', true);    
        xmlhttp.send(fd);
    });
}

最新更新