Download FormData from Trigger.io Forge on Amazon S3

I am trying to download an image file from my Trigger.io mobile application directly to Amazon S3 (see here http://aws.amazon.com/articles/1434 ). I can do this on the Internet without any problems using jQuery and the API FormDataas follows:

var fd = new FormData();
key = 'test.jpg'

fd.append('key', key);
fd.append('acl', 'public-read');
fd.append('Content-Type', file.type);
fd.append('AWSAccessKeyId', key_id);
fd.append('policy', policy_base64);
fd.append('signature', signature);
fd.append('file', file);

$.ajax({
    type: 'POST',
    url: 'https://' + bucket + '.s3.amazonaws.com/',
    processData: false, // Not supported with Trigger
    contentType: false, // Not supported with Trigger
    data: fd,
    success: function(response) {
        // It worked...
    }
});

However, I cannot get this to work with the Forge request APIs. This is what I tried:

forge.request.ajax({
    type: 'POST',
    url: 'https://' + bucket + '.s3.amazonaws.com/',
    fileUploadMethod: 'raw',
    files: [file],
    data: fd,
    headers: { 'Content-Type': 'multipart/form-data', 'x-amz-acl': 'public-read' },
    success: function(response) {
        // It worked...
    }
});

But I get the following error from Amazon:

<Code>PreconditionFailed</Code>
<Message>At least one of the pre-conditions you specified did not hold</Message>
<Condition>Bucket POST must be of the enclosure-type multipart/form-data</Condition>

I will get around this forge.requestcompletely in favor $.ajax, but mine filewas obtained using the Forge API files and just displays on S3 as [object Object](I assume this is a Forge file, not a true HTML file object <input />).

, Trigger.io Amazon S3 FormData Forge API? ! !

+3
1

jQuery ajax $.ajax API- FormData JavaScript Trigger.io Amazon S3, .

:

  • API forge.file.
  • forge.file.base64, base64 .
  • JavaScript Blob base64
  • Blob FormData
  • $.ajax POST Amazon S3

, ( 1), uploadImage Amazon S3:

function uploadImage(file, awsAccessKeyId, policy, signature, bucket) {
    forge.file.base64(file, function (base64String) {
      // Create a Blob from a base64 string
      var byteCharacters = atob(base64String);
      var byteNumbers = new Array(byteCharacters.length);
      for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
      }
      var byteArray = new Uint8Array(byteNumbers);
      var blob = new Blob([byteArray.buffer], {type: "image/jpeg"});

      var fd = new FormData();
      fd.append('key', 'test.jpg');
      fd.append('acl', 'public-read');
      fd.append('Content-Type', 'image/jpeg');
      fd.append('AWSAccessKeyId', awsAccessKeyId);
      fd.append('policy', policy);
      fd.append('signature', signature);    
      fd.append("file", blob);

      $.ajax({
        type: 'POST',
        url: 'http://' + bucket + '.s3.amazonaws.com/',
        processData: false,
        contentType: false,
        data: fd,
        success: function(response) {
          alert('Success uploading photo');
        },
        error: function () {
          alert('Problem uploading photo');
        }
     });    
  });
}
+7

All Articles