If you do not need significantly large files, or perhaps only storing files for a short period of time, then this simple solution works very well.
In your html ...
<input id="files" type="file" />
In your template event map ...
Template.template.events({ 'submit': function(event, template){ event.preventDefault(); if (window.File && window.FileReader && window.FileList && window.Blob) { _.each(template.find('#files').files, function(file) { if(file.size > 1){ var reader = new FileReader(); reader.onload = function(e) { Collection.insert({ name: file.name, type: file.type, dataUrl: reader.result }); } reader.readAsDataURL(file); } }); } } });
Subscribe to the collection and visualize the link in the template ...
<a href="{{dataUrl}}" target="_blank">{{name}}</a>
Although this may not be the most reliable or elegant solution for large files or an application with an intensive file, it works great for all types of file formats if you want to implement simple file upload and download / rendering.
Steeve Cannon Sep 06 '14 at 22:27 2014-09-06 22:27
source share