How can I get on server an uploaded picture (using ngCordova) from Ionic app?


#1

Hi! I use this function to upload an image to a server running node.js and express.

var uploadPhoto = function(imageURI) {
  var server = "http://192.168.1.62:8080/upload";
  var filePath = imageURI;
  var options = {};

  $cordovaFileTransfer.upload(server, filePath, options).then(function (result) {
    //
  }, function (err) {
    alert('error');
  }, function(progress) {
    //
  });
};

First question: What properties should be added to the options object above?
Second question: How can I get the image on the server?

router.post('/upload', function(req, res, next) {

  // How can I extract the image from the request (and save it)?
  // I have to mention that: req.body = {};
  res.send("ok");
});

#2

If you are using express, then use connect-multiparty module. And in your code do something like this (This is example from y app where I upload file to amazon):

In my node controller:

uploadService.upload(req.files.file, req.files.file.name,
    function(fileObj){
      return res.jsonp(fileObj);
    },
    function(err){
      return res.jsonp(500, err);
    }
  ); 

And my uploadService

module.exports = {

upload: function(file, name, success, error) {
var path = file.path;
var destFileName = shortId.generate()+’.’+mime.extension(file.type);

if (name) {
  var b = new Buffer(name);
  var s = b.toString('base64');
  destFileName = s+'.'+mime.extension(file.type);
}

fs.stat(path, function(err, file_info) {
  var bodyStream = fs.createReadStream( path );

  var params = {
    Bucket: bucket,
    ACL: 'public-read',
    Key: destFileName,
    ContentLength : file_info.size,
    Body: bodyStream
  };

  s3.putObject(params, function(err, data) {
    if (err) {
      error(err);
    } else {
      success({
        url: util.format('http://%s.s3.amazonaws.com/%s', bucket, destFileName),
        key: destFileName,
        size: file_info.size
      });
    }
  });

});
},

#3

I am also using express, is that module the only way of getting the image data?


#4

Never mind for the dumb question after seconds of plugin in the module it was a success thanks !