IONIC:从Cordova Camera单击将图像上传到Cloudinary

问题描述 投票:1回答:1

[经过几个小时的尝试,我决定在此问题上寻求社区的帮助。我正在开发一个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);
            });
};
    };
cordova ionic-framework cloudinary
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.