Ember.js: Upload file component

后端 未结 4 1256
梦如初夏
梦如初夏 2021-02-05 23:40

I need to create an Ember component to select a file. My page will include multiple \"upload component\"

I have read a post trying to implement that: (https://stackoverf

4条回答
  •  执念已碎
    2021-02-06 00:37

    I completed a full blown example to show this in action

    https://github.com/toranb/ember-file-upload

    Here is the basic handlebars template

    
    

    Here is the custom file view object

    PersonApp.UploadFileView = Ember.TextField.extend({
        type: 'file',
        attributeBindings: ['name'],
        change: function(evt) {
          var self = this;
          var input = evt.target;
          if (input.files && input.files[0]) {
            var reader = new FileReader();
            var that = this;
            reader.onload = function(e) {
              var fileToUpload = reader.result;
              self.get('controller').set(self.get('name'), fileToUpload);
            }
            reader.readAsDataURL(input.files[0]);
          }
        }
    });
    

    Here is the controller

    PersonApp.PersonController = Ember.ObjectController.extend({
      content: null,
      logo: null,
      other: null
    });
    

    And finally here is the view w/ submit event

    PersonApp.PersonView = Ember.View.extend({
      templateName: 'person',
      submitFileUpload: function(event) {
        event.preventDefault();
        var person = PersonApp.Person.createRecord({ username: 'heyo', attachment: this.get('controller').get('logo'), other: this.get('controller').get('other') });
        this.get('controller.target').get('store').commit();
      }
    });
    

    This will drop 2 files on the file system if you spin up the django app

提交回复
热议问题