IONIC:从Cordova Camera上传图片点击Cloudinary

时间:2015-07-07 09:21:49

标签: cordova ionic cloudinary

经过几个小时的努力,我决定在这个问题上寻求社区帮助。我正在开发一个IONIC应用程序,我需要将图像从Cordova Camera上传到Cloudinary。现在我们可以通过单击相机中的图像(在缓存中加载图像)或选择已保存在图库中的图像来实现。我可以点击图片并从图库中选择它,但我无法将图像二进制文件上传到Cloudinary。

我尝试通过' $ cordovaCamera'' $ cordovaFile'发送二进制数据。并通过传递URL但双向失败。有什么我想念的吗? 这是我为相机配置的内容:

vm.takePicture = function () {
            var options = { 
                quality : 100, 
                destinationType : Camera.DestinationType.DATA_URL, 
                //destinationType: Camera.DestinationType.FILE_URI,
                sourceType : Camera.PictureSourceType.CAMERA, 
                allowEdit : true,
                encodingType: Camera.EncodingType.JPEG,
                targetWidth: 300,
                targetHeight: 300,
                popoverOptions: CameraPopoverOptions,
                saveToPhotoAlbum: false
                //saveToPhotoAlbum: true
            };

 vm.UploadPicture = function () {
            vm.uploadinProgress = true;
            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = Image.jpeg;
            options.chunkedMode = false;
            options.trustAllHosts = true;
 $cordovaFile.UploadFile("http://myserver.com/images", vm.imgSrc, options).then(function(result) {
                console.log("Success");
            },function error() {
                console.log('Error: ' + error);
            });
};
    };

1 个答案:

答案 0 :(得分:3)

我选择将图片直接上传到cloudinary,以免上传服务器上传的额外开销。

这可以通过"直接上传"来实现。 cloudinary的功能,专为移动设备而设计。你需要设置" upload_preset"您发布请求中的参数。

//选择摄像头捕获的设置

 vm.takePicture = function () {
            var options = {
                quality: 50,
                destinationType: Camera.DestinationType.FILE_URI,
                sourceType: Camera.PictureSourceType.CAMERA,
                encodingType: Camera.EncodingType.JPEG,
            };

//设置图库图片

vm.selectPicture = function () {
            var options = {
                quality: 50,
                allowEdit: false,
                destinationType: Camera.DestinationType.FILE_URI,
                sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
                encodingType: Camera.EncodingType.JPEG
            };

//然后只需使用IONIC文件传输库

 var ft = new FileTransfer();
 ft.upload(vm.Image, server, win, fail, ftOptions, true);
相关问题