Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
add comment

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.