【问题标题】:Ionic - take a picture with camera and send it to Amazon S3Ionic - 用相机拍照并将其发送到 Amazon S3
【发布时间】:2015-09-28 12:12:33
【问题描述】:

我正在使用 Ionic 框架(angularjs + cordova)构建一个应用程序。

我希望用户能够用他的手机摄像头拍照,并将其直接发送到亚马逊 aws 上的 S3 存储。

我知道如何拍照,并在本地文件系统上获取 base64 和 URI:

ngcordova - camera

我也知道如何使用 jquery 通过 ajax 将文件发送到 S3

Direct upload to S3

现在,问题是我发现的所有文件上传都使用“输入类型=文件”。如何使用本地 URI 或 base64 编码字符串将文件传递给上传者?

【问题讨论】:

标签: jquery angularjs amazon-web-services amazon-s3 ionic-framework


【解决方案1】:

我让它工作,通过将相机插件返回的文件转换为 blob,因为文件继承自 blob,我可以通过 S3 上传器通过定义一个虚假的本地文件名来发送它

这是构建 blob 的部分

        $cordovaCamera.getPicture(options).then(function(imageUrl) {

            var getFileBlob = function (url, cb) {
              var xhr = new XMLHttpRequest();
              xhr.open("GET", url);
              xhr.responseType = "blob";
              xhr.addEventListener('load', function() {
                  cb(xhr.response);
              });
              xhr.send();
            };
            var blobToFile = function (blob, name) {
                blob.lastModifiedDate = new Date();
                blob.name = name;
                return blob;
            };
            var getFileObject = function(filePathOrUrl, cb) {
               getFileBlob(filePathOrUrl, function (blob) {
                  cb(blobToFile(blob, 'test.jpg'));
               });
            };

            //File created
            getFileObject(imageUrl, function (fileObject) {
               deferred.resolve({imageUrl : imageUrl, fileObject : fileObject});
            }); 
          }, function(err) {
            // error
            console.warn(err);
            deferred.reject(err);
          });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多