Search code examples
javascriptangularjsmobilecordovacordova-3

Angularjs + Cordova + UploadFile convert Img to file to do a POST


I'm developing a Cordova 3.3.0 app + Angularjs.

I'm trying to do a multipart POST. The server is waiting for a file, I can't upload base64 string and I have the real path of the image stored in device.

Also i have the base64 image if I need to use it.

Input file is not supported in cordova. Obviosly I can't set the value to the input file for security reasons.

So I have to convert the or base64 to a file and then do the post.

//convert DOM img to file

var data ={

'lumen_moore_editar_usuario_rest[gender]':'M',
'lumen_moore_editar_usuario_rest[file]':file
          };

$http({url: WS_EDIT_USER, method: 'POST',
                data: $.param(data),
                transformRequest: angular.identity,
                headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
            }).success(function(data, status, headers, config) {
                    console.log("EDITAR IMAGEN SUCCESS "+ data);
                }).error(function(data, status, headers, config) {
                    console.log("EDITAR IMAGEN ERROR "+ data);
                });

Is it possible??. I was looking alot this answer but not luck and I don't want to use FileTransfer.upload of cordova.

Sorry for my english.

Thanks!


Solution

  • I had to use Cordova FileTransfer and it worked at first time. It's wrapped in a service with a promise.

    editProfileUserImg:function(imageURI){
    
                var deferred = $q.defer();
    
    
                                var options = new FileUploadOptions();
                                options.fileKey="file";
                                options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
    
                                var params = {};
    
                                options.params = params;
                                var headers={ 'Authorization':ACCESS_TOKEN.Authorization,
                                              'X-WSSE':ACCESS_TOKEN.XWSSE};
    
                                options.headers = headers;
    
    
                                var ft = new FileTransfer();
                                ft.upload(imageURI, encodeURI(WS_CHANCE_PIC_USER),
                                    function(r){
                                        console.info("Code = " + r.responseCode);
                                        console.info("Response = " + r.response);
                                        console.info("Sent = " + r.bytesSent);
                                        deferred.resolve(r.response);
    
                                    },
                                    function(error){
                                        alert("An error has occurred: Code = " + error.code);
                                        console.error("upload error source " + error.source);
                                        console.error("upload error target " + error.target);
                                        deferred.reject(error);
    
                                    }, options);
    
    
                return deferred.promise;
            }