如何使用提取文件的自定义Angular指令上传到S3?

时间:2015-11-13 23:23:18

标签: javascript angularjs amazon-s3

所以我使用以下设置允许用户通过自定义Angular指令选择要上传的文件:

http://jsfiddle.net/JeJenny/ZG9re/

我尝试使用控制器方法中的以下AngularJS代码上传到Amazon S3:

let someString = "\(someFloat)"
let someString2 = NSString(format: "%.3f", someFloat2) 

请注意,formData中的 file 对应于通过jsfiddle中的custom指令上传的文件。

运行上述POST请求后,Chrome开发工具控制台会显示400错误消息:

var formData = {
          'key': res.key,
          'policy': res.policy,
          'signature': res.signature,
          'acl': 'public-read',
          'success_action_status': '201',
          'AWSAccessKeyId': res.AWSAccessKeyId,
          'file': file
        };
        $http({
          url: formUrl,
          method: "POST",
          data: formData,
          transformRequest: angular.identity,
          headers: {'Content-Type': 'multipart/form-data' }
        }).success(function(data, status, headers, config) {
          console.log(status);
          console.log(file.type);
        }).error(function(data, status, headers, config) {
          console.log('Error: ');
          console.log(data);
        });

我该如何解决这个问题?我是否需要将文件转换为其他格式?

以下是控制台中显示的与S3相关的formData参数的清理副本,我将其作为错误响应的一部分返回:

The body of your POST request is not well-formed multipart/form-data.

以下是从Chrome开发工具中复制的400 POST请求标头:

AWSAccessKeyId: "AKXXDFDSFABGCADF7Q"
Acl: "public-read"
Success-Action-Status: "201"
key: "2020/blah_person_342_sy_18_"
policy: "eyJleHBpcmF0axxxxxxxx25kaXRpb25zIjpbeyJidWNrZXQiOiJzY2N0c2ktZGF0YW5leHVzLXN0YWdpbmcifSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIiJdLHsiYWNsIjoicHVibGljLXJlYWQifSx7InN1Y2Nlc3NfYWN0aW9uX3N0YXR1cyI6IjIwMSJ9LFsic3RhcnRzLXdpdGgiLCIkcGVyc29uX2lkIiwiIl0sWyJzdGFydHMtd2l0aCIsIiRzdXJ2ZXlfaWQiLCIixxxxxxxoIiwiJGF1dGhlbnRpY2l0eV90b2tlbiIsIiJdLFsiY29udXXYHC1sZW5ndxxxxxFuZ2UiLDAsNTAwMDAwMDBdXX0="
signature: "wwxxxxx5Uh+6kSuLF2xxxxujk=

0 个答案:

没有答案