2
\$\begingroup\$

I am a newbie and tried to find the laziest way to do a file upload using EmberJS, jQuery and formdata (IE10+). The code might look stupid, but it worked.

Can you please take a look and give some suggestions? Am I doing it wrong?

<script type="text/x-handlebars" data-template-name="posts">

<form role="form" enctype="multipart/form-data" method="post" id="fileinfo" {{action    'createPost' on='submit'}}>

  {{input type="text" value=newPost  id="newPost" placeholder="Post" class="form-control"}}

  {{input type="file" id="inputFile"  class="form-control" name="file"}}

  <button type="submit" class="btn btn-default" >Submit</button>

</form>

</script>


App.PostsController = Ember.ArrayController.extend({
actions: {
    createPost: function(){
                    var fd = new FormData(document.getElementById("fileinfo"));
                    fd.append("postContent", this.get('newPost'));
                    this.set('newPost', ''); //reset text field
                    $('#inputFile').val(''); //reset fileinput field

                    Ember.$.ajax({
                      url: "http://localhost:3000/posts",
                      type: "POST",
                      data: fd,
                      processData: false,  // tell jQuery not to process the data
                      contentType: false,   // tell jQuery not to set contentType
                    });

                    }   
                 }
             });
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.