scriptology
1/27/2015 - 9:36 PM

Controller does not see the incoming files. What could be wrong?

Controller does not see the incoming files. What could be wrong?

<!-- /file/upload -->
<input type="file" name="uploadFile" onchange="angular.element(this).scope().uploadFile2(this.files)"/>
module.exports = {
  upload: function  (req, res) {

    if(req.method === 'GET')
      return res.json({'status':'GET not allowed'});

    //var uploadFile = req.file('uploadFile');
    //sails.log.debug(uploadFile);

    req.file('uploadFile').upload(function (err, uploadedFiles) {
      if (err) return res.send(500, err);
      return res.json({
        message: uploadedFiles.length + ' file(s) uploaded successfully!',
        files: uploadedFiles
      });
    });

  }
};
$scope.uploadFile2 = function(files) {
  var fd = new FormData();
  //Take the first selected file
  fd.append("uploadFile", files[0]);

  $http.post('/file/upload?_csrf=' +  encodeURIComponent($('.csrf').val()), fd, {
    withCredentials: true,
    headers: {'Content-Type': "multipart/form-data; charset=UTF-8; boundary=frontier" },
    transformRequest: angular.identity
  }).success(" ...all right!... ").error(" ..damn!... ");

};
debug: { _files: [],
  timeouts: 
   { untilFirstFileTimer: 
      { _idleTimeout: 500,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 1422395832312,
  ...
  ...
  ...
  fieldName: 'uploadFile' }
------WebKitFormBoundarywwWyGegawRJj6D2m
Content-Disposition: form-data; name="uploadFile"; filename="avatar.png"
Content-Type: image/png
------WebKitFormBoundarywwWyGegawRJj6D2m--