How to upload image to nodeJS Server?


#1

I’m trying to get $cordovaFileTransfer to upload some images to a nodeJS Express server, but I need some help.

On ionic I have the following code and I can get the selected images and send it to the server.

$cordovaImagePicker.getPictures(options)
    .then(function (results) {
      for (var i = 0; i < results.length; i++) {
        console.log('Image URI: ' + results[i]);
        $cordovaFileTransfer.upload('http://10.0.0.92:3000/upload', results[i])
          .then(function (result) {
            console.log('success');
          }, function (error) {
            console.log('what');
          });

      }
    }, function(error) {
      // error getting photos
    });

On my express server I have the following code.

var express = require('express'),
    app = express(),
    path = require('path'),
    multer  = require('multer'),
    fs = require('fs');

var updLocation = path.join(__dirname, '/gallery');
var upload = multer({ dest:updLocation});

app.post('/upload', upload.array('photos'), function(req, res, next){
   console.log(req);
});

I keep getting the following error on the nodeJS Server when I try to upload the files. Anything else I need to do?

Server listening on port 3000
Error: Unexpected field
at makeError (/Users/enniobozzetti/WebstormProjects/demo/node_modules/multer/lib/make-error.js:12:13)
at wrappedFileFilter (/Users/enniobozzetti/WebstormProjects/demo/node_modules/multer/index.js:39:19)
at Busboy. (/Users/enniobozzetti/WebstormProjects/demo/node_modules/multer/lib/make-middleware.js:113:7)
at emitMany (events.js:127:13)
at Busboy.emit (events.js:201:7)
at Busboy.emit (/Users/enniobozzetti/WebstormProjects/demo/node_modules/busboy/lib/main.js:31:35)
at PartStream. (/Users/enniobozzetti/WebstormProjects/demo/node_modules/busboy/lib/types/multipart.js:213:13)
at emitOne (events.js:96:13)
at PartStream.emit (events.js:188:7)
at HeaderParser. (/Users/enniobozzetti/WebstormProjects/demo/node_modules/dicer/lib/Dicer.js:51:16)


#2

I got it working. The issue was with the default file name