I'm looking to use Multer to upload an image file onto my node server. The code is simple, I copied it from Multer's docs. I spent the whole day stackoverflowing this issue and it always comes back to giving me undefined for using ajax to send the image.
Using a form DOES work: multer - req.file always undefined
However, I'm curious to see if anyone have successfully used ajax way to sending files.
HTML
<input type="file" id="avatar" name="avatar"/>
When the user clicks on the button and chooses a file, I simply display the file info, and use ajax to send the data to my node router http://localhost:8882/profile
js
<script>
//files structure contains data about each file you load in
   document.getElementById('avatar').addEventListener('change', function() {
    var file = this.files[0];
    console.dir(this.files[0]);
    postItem(this.files[0]);
}, false);
function postItem(imageObj) {
  console.log("postItem <----");
    $.ajax({
        type: "POST",
        url: "http://localhost:8882/profile",
        contentType:false,
        cache: false,
        processData: false,
        data:  imageObj,
        success: function(data) {
            //show content
            alert('Success!');
        },
        error: function(jqXHR, textStatus, err) {
            //show error message
            alert('text status ' + textStatus + ', err: '+err);
        }
    }); //ajax
}//postItem()
</script>
On the Node side, the code is directly from multer's docs: https://www.npmjs.com/package/multer
I keep getting undefined for req.file.
Node
var express     = require('express');
var app         = express();
var bodyParser  = require('body-parser');
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })
app.post('/profile', upload.single('avatar'), function (req, res, next) {
    console.log("------ /profile --------");
    if(req) {
        console.dir(req.file);
    }
    console.log("------------------------");
    res.send({"message":"ok"});
});
app.listen(8882);
console.log("index.js - listening on port " + 8882);
exports.app = app;
Would appreciate any help.
 
     
     
    