mardi 18 avril 2017

upload a file using ember-uploader

I'm trying to figure out how to use ember-uploader, I have the following component (like the one in the README)

export default EmberUploader.FileField.extend({
  filesDidChange: function(files) {
    const uploader = EmberUploader.Uploader.create({
      url: (ENV.APP.API_HOST || '') + '/api/v1/images/',
    });
    console.log(uploader);

    if (!Ember.isEmpty(files)) {
      var photo = files[0];
      console.log(photo);

      uploader.upload(photo)
        .then(data => {
          // Handle success
          console.log("Success uploading file");
          console.log(data);
        }, error => {
          // Handle failure
          console.log("ERROR uploading file");
          console.log(error);
        });
    }
  }
});

The express API endpoint is listening for a POST request.

var saveImage = (req, res, next) => {
    let body = req.body;
    res.json({
      data: body
    });
};

But the body is empty after the request is done. I really don't know how to implement the API endpoint in order to get the file, I tried to see the req object and it doesn't contains the file.

Debugging it, After select a file using the component I get the following info in the console.

js console

Seems that the API endpoint works because I get the following output:

POST /api/v1/images/ 200 27.284 ms - 11

But I can't get the file.




Aucun commentaire:

Enregistrer un commentaire