vendredi 12 mai 2017

How can I upload an image using Ember Data, Express and Multer?

I am building out an account page in an Ember app where someone can update their account.

I have built out an Ember component for my form and I'm trying to upload an image to an API built with Express.

I'm trying to use Multer to handle the image upload but it doesn't seem to be working.

My Form

<form enctype="multipart/form-data" >
    <label>Avatar</label>
    
    <input type="hidden" value="" name="user_id" />
</form>

My Ember Component

export default Ember.Component.extend({
    store: Ember.inject.service('store'),
    actions: {
        update: function(form) {

            var userID = form.$('[name="user_id"]').val();

            this.get('store').findRecord('user', userID).then(function(user) {
                // finished
            });
        }
    }
});

My Express Route

var multer = require('multer'),
    upload = multer({dest: '../public/img'});

app.put('/users/:id', upload.any(), function(req, res) {

    console.log(req.files);
});

This always results in undefined. Is this because Multer doesn't handle PUT requests? Is it because Ember Data doesn't support multipart/form-data forms?

I can only find examples of POST requests using Multer but seeing as Ember Data defaults to PUT requests when doing save(), I have to assume that is standard for updating records. What am I missing?




Aucun commentaire:

Enregistrer un commentaire