angular file upload with nervgh/angular-file-upload

1.2k views Asked by At

I'm doing a file upload from angular implementing the https://github.com/nervgh/angular-file-upload plugin, my setup it's like this :

var vm = this,
    apiUrl = appConfig.getItem('BASE_API_URL'), // base url;

vm.uploader = $scope.uploader = new FileUploader({
    url: apiUrl + '/invoices/upload',
    headers: {
      "Content-Type": undefined
    }
});

// this function is triggered by a button outside
vm.uploadAll = function () { 
  vm.uploader.uploadAll();
};

on the html I have

    <input id="uploadFileButton"
       type="file"
       nv-file-select
       uploader="FUCtrl.uploader"
       multiple
       style="display: none;"/> 
    // the display none is due to that this input is click triggered
    // by an outside button

the thing is that for start on the client side the post request I see this enter image description here

an image is uploaded (in theory), but the Content-Type is undefined, a missing enctype, and on the other hand on the server side I have this

var express = require('express'),
    multer  = require('multer'),
    cors = require('cors');

var app = express();

app.use(cors());
app.get('*', function(){});

app.use(multer({dest:'./uploads/'}).single('photo'));

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

  console.log('hit');

  console.log(req.body); // form fields
  console.log(req.files); // form files
  res.status(204).end();

});

app.listen(3000);

but when I recieve the post I see on the console console.log(req.body); // {} console.log(req.files); // undefined

and I can't get any data from the pdf's upload

what am I missing ?

2

There are 2 answers

0
Franco Aguilera On BEST ANSWER

Finally I've discoverded the solution, on the HTML

nv-file-select

should be

nv-file-select=""

and remove

headers: {
  "Content-Type": undefined
}

from the uploader configuration, apparently those things didn't set the content-type and his boundary well, so I toke them out and goy it to work

2
Bhautik On

hey man i don't know about that plugin. but i am using this https://github.com/danialfarid/ng-file-upload plugin and i find very helpful. this is the easiest way to upload a file.

Upload with form submit and validations:

http://jsfiddle.net/danialfarid/maqbzv15/1118/

Upload multiple files one by one on file select:    

http://jsfiddle.net/danialfarid/2vq88rfs/136/

Upload multiple files in one request on file select (html5 only): 

http://jsfiddle.net/danialfarid/huhjo9jm/5/

Upload single file on file select: 

http://jsfiddle.net/danialfarid/0mz6ff9o/135/

Drop and upload with $watch: 

http://jsfiddle.net/danialfarid/s8kc7wg0/400/

Image Crop and Upload 

http://jsfiddle.net/danialfarid/xxo3sk41/590/