拍照并用离子上传到S3

时间:2015-09-08 22:32:29

标签: angularjs cordova amazon-s3 ionic-framework ionic

我正在尝试开发一个应用程序来拍照并使用ionic - cordova将其上传到Amazon S3服务器。

我可以拍照并将文件发送到AWS S3,但我认为我发送错误的身体数据作为参数。

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }
  });
})

.config(function($compileProvider){
  $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|tel):/);
})

.controller('MainCtrl', function($scope, $http, Camera) {

  $scope.getPhoto = function() {
    console.log("got camera button click");

    Camera.getPicture().then(function(imageURI) {
      console.log(imageURI);
      $scope.lastPhoto = imageURI;
      var   ft = new FileTransfer(),
            reader = new FileReader(),
            fileName,
            options = new FileUploadOptions();
        options.fileKey="ffile";
      fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.fileName=fileName;
        options.mimeType="image/jpeg";
        var params = {};
      options.fileKey = "file";
      options.chunkedMode = false;
      console.log(fileName);


      $http.get('http://192.168.0.10:3000/signing', {params:{"fileName": fileName}}).then(
         function(resp) {
            reader.onloadend = function(e) {
                var reader = new FileReader();
               console.log(this.result);

                AWS.config.update({ accessKeyId: resp.data.awsKey, secretAccessKey: resp.data.secretAccessKey });
                AWS.config.region = 'us-east-1';
                var bucket = new AWS.S3({ params: { Bucket: resp.data.bucket } });
                var params = { Key: fileName, ContentType: "image/jpeg", Body: this.result, ServerSideEncryption: 'AES256' };

                bucket.putObject(params, function(err, data) {
                  if(err) {
                    // There Was An Error With Your S3 Config
                    alert(err.message);
                    return false;
                  }
                  else {
                    // Success!
                    alert('Upload Done');
                  }
                }); //End putObject
              } //end onloadend
          var blob = dataURItoBlob(imageURI);
          reader.readAsBinaryString(blob);



          }, function(err) {
            console.err(err);
          }, {
            quality: 75,
            targetWidth: 320,
            targetHeight: 320,
            saveToPhotoAlbum: false,
             destinationType: 0
          }); //End HTTP.GET
        }); // End Get Picture
  } //end GetPhoto
  function dataURItoBlob(dataURI) {
    // convert base64/URLEncoded data component to raw binary data held in a string
    var byteString;
    if (dataURI.split(',')[0].indexOf('base64') >= 0)
        byteString = atob(dataURI.split(',')[1]);
    else
        byteString = unescape(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

    // write the bytes of the string to a typed array
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    return new Blob([ia], {type:mimeString});
}
}) //End controller

0 个答案:

没有答案