【问题标题】:Ionic Take pictures cannot upload to cloudinary using unsigned upload离子拍照无法使用未签名上传上传到云端
【发布时间】:2016-05-16 06:03:41
【问题描述】:

我正在尝试使用cordova插件拍照并使用离子浏览器上传到cloudinary。我在 cloudinary 中设置了未签名的上传,并且基本上从https://calendee.com/2015/01/15/posting-images-to-cloudinary-in-ionic-apps/ 中获取了逻辑 上传到cloudinary。 我在 DashCtrl 控制器中编写了所有逻辑,只是为了方便查看所有内容。代码如下:

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope, $cordovaCamera, $cordovaGeolocation, $cordovaFileTransfer, $q, $base64, $translate) {
  //$scope.$inject = ['$cordovaCamera','$cordovaGeolocation','$cordovaFileTransfer'];
  $scope.imageURI = '';
  $scope.log=function(){
    console.log('hello~~~');
  };


  $scope.takePicture = function() {
    console.log('taking pictures ....');
        var options = {
            quality : 75,
            destinationType : Camera.DestinationType.DATA_URL,
            sourceType : Camera.PictureSourceType.CAMERA,
            allowEdit : true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function(imageData) {
            $scope.imageURI =  imageData;

            //console.log('$scope.image is ', $scope.imageURI);
            var base64EncodedString = $base64.encode(imgURI);
            var base64EncodedString = decodeURIComponent(base64EncodedString);
            var decodedString = $base64.decode(base64EncodedString);
            return uploadImage(decodedString);
        })
        .then(function(result){
          var url = result.secure_url || '';
          var urlSmall;

          if(result && result.eager[0]) {
            urlSmall = result.eager[0].secure_url || '';
            console.log('url ~~~~~~~~ is ', urlSmall);
            chat.sendMessage(roomId,'', 'default', urlSmall, function(result){
              console.log('url is ', urlSmall);
              console.log('message image url successfully updated to firebase');
            })
          }

          // Do something with the results here.

          $cordovaCamera.cleanup();
        }, function(err){
          // Do something with the error here
          $cordovaCamera.cleanup();
        });

  };

  function uploadImage(imageURI) {
        var deferred = $q.defer();
        var fileSize;
        var percentage;
        // Find out how big the original file is
        window.resolveLocalFileSystemURL(imageURI, function(fileEntry) {
          fileEntry.file(function(fileObj) {
            fileSize = fileObj.size;
            // Display a loading indicator reporting the start of the upload
            $ionicLoading.show({template : 'Uploading Picture : ' + 0 + '%'});
            // Trigger the upload
            uploadFile();
          });
        });
        function uploadFile() {
          // Add the Cloudinary "upload preset" name to the headers
          var uploadOptions = {
            params : { 'upload_preset': MY_PRESET}
          };
          $cordovaFile
            // Your Cloudinary URL will go here
            .uploadFile(MY_UPLOAD_URL, imageURI, uploadOptions)

            .then(function(result) {
              // Let the user know the upload is completed
              $ionicLoading.show({template : 'Upload Completed', duration: 1000});
              // Result has a "response" property that is escaped
              // FYI: The result will also have URLs for any new images generated with 
              // eager transformations
              var response = JSON.parse(decodeURIComponent(result.response));
              deferred.resolve(response);
            }, function(err) {
              // Uh oh!
              $ionicLoading.show({template : 'Upload Failed', duration: 3000});
              deferred.reject(err);
            }, function (progress) {
              // The upload plugin gives you information about how much data has been transferred 
              // on some interval.  Use this with the original file size to show a progress indicator.
              percentage = Math.floor(progress.loaded / fileSize * 100);
              $ionicLoading.show({template : 'Uploading Picture : ' + percentage + '%'});
            });
        }
        return deferred.promise;
  }


})

我可以拍照,但仅此而已。 uploadImage 中的 console.log 从未出现。为我的新手离子知识道歉,但我真的不知道在哪里解决这个问题。我配置为发布到这个 cloudniary url:“http://res.cloudinary.com/MY_DOMAIN/image/upload”。

完整代码在这个仓库中:https://github.com/7seven7lst/Ionic_test/

【问题讨论】:

  • 虽然 Cloudinary 没有提供与 Cordova 和 Ionic 的官方集成,但请注意上传到 Cloudinary 的端点是:https://api.cloudinary.com/v1_1/<cloud_name>/image/upload,而不像您在代码中使用的那样。看看有没有帮助。
  • 我试过这个。我遇到的问题仍然是一样的。有消息:“对尚未渲染的视图进行快照会导致快照为空。请确保您的视图在快照之前或屏幕更新后的快照之前已至少渲染一次。”
  • 你能确认uploadImage 被解雇了吗?
  • 嗨纳达夫。 uploadImage 没有被解雇。我将研究拍照的配置。谢谢。

标签: cordova ionic-framework cloudinary


【解决方案1】:

你正在使用

$cordovaFile.uploadFile(MY_UPLOAD_URL, imageURI, uploadOptions)

你需要使用

 $cordovaFileTransfer.upload(server, filePath, options)

这里有更多信息:

CordovaFileTransfer

【讨论】:

    猜你喜欢
    • 2018-09-18
    • 2013-09-13
    • 2017-02-07
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多