It turns out this is quite easy to do, although a bit verbose. You need to:
- Create a FileReader.
- Set an event handler for the onload event.
- Pass the file(s) you want to read to the appropriate reading method.
MyApp.Views.file = Ember.View.extend
tagName: 'input'
attributeBindings: ['type', 'id']
type: 'file'
change: (e)->
view = this
reader = new FileReader()
reader.onload = (e)->
view.set('file', e.target.result)
reader.readAsText(e.target.files[0])
This can then be invoked in a handlebars template as follows:
{{view MyApp.Views.file fileBinding="SomeModel.attribute" }}
I expect this approach to be unsuitable for extremely large files as it will read the entire file into memory and transmit it as part of the payload rather than as a file upload, for some value of extremely large...
No comments:
Post a comment