Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
                    });

                    }   
                 }
             });
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.