🚀 Upload files via DDP or HTTP to ☄️ Meteor server FS, AWS, GridFS, DropBox or Google Drive. Fast, secure and robust.
Stable, fast, robust, and well-maintained Meteor.js package for files management using MongoDB Collection API. What does exactly this means? Calling
.insert()method would initiate a file upload and then insert new record into collection. Calling
.remove()method would erase stored file and record from MongoDB Collection. And so on, no need to learn new APIs. It's flavored with extra low-level methods like
.unlink()and
.write()for complex integrations.
Meteor-Files?
HTTPand
DDPtransports, read about difference
onBeforeUploadhook
onAfterUploadhook, and manage file's subversions in a single record
meteor add ostrio:files
import { FilesCollection } from 'meteor/ostrio:files';
new FilesCollection([config])[Isomorphic]
FilesCollectionConstructor
Shared code:
import { Meteor } from 'meteor/meteor'; import { FilesCollection } from 'meteor/ostrio:files';const Images = new FilesCollection({ collectionName: 'Images', allowClientCode: false, // Disallow remove files from Client onBeforeUpload(file) { // Allow upload files under 10MB, and only in png/jpg/jpeg formats if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) { return true; } return 'Please upload image, with size equal or less than 10MB'; } });
if (Meteor.isClient) { Meteor.subscribe('files.images.all'); }
if (Meteor.isServer) { Meteor.publish('files.images.all', function () { return Images.find().cursor; }); }
insert(settings[, autoStart])[Client]
insert()method
Upload form (template):
Shared code:
import { FilesCollection } from 'meteor/ostrio:files'; const Images = new FilesCollection({collectionName: 'Images'}); export default Images; // import in other files
Client's code:
import { Template } from 'meteor/templating'; import { ReactiveVar } from 'meteor/reactive-var'; Template.uploadForm.onCreated(function () { this.currentUpload = new ReactiveVar(false); });Template.uploadForm.helpers({ currentUpload() { return Template.instance().currentUpload.get(); } });
Template.uploadForm.events({ 'change #fileInput'(e, template) { if (e.currentTarget.files && e.currentTarget.files[0]) { // We upload only one file, in case // multiple files were selected const upload = Images.insert({ file: e.currentTarget.files[0], chunkSize: 'dynamic' }, false);
upload.on('start', function () { template.currentUpload.set(this); }); upload.on('end', function (error, fileObj) { if (error) { alert(`Error during upload: ${error}`); } else { alert(`File "${fileObj.name}" successfully uploaded`); } template.currentUpload.set(false); }); upload.start(); }
} });
For multiple file upload see this demo code.
Upload base64 string (introduced in v1.7.1):
// As dataURI Images.insert({ file: 'data:image/png,base64str…', isBase64: true, //For more expressive example see Upload demo app
Stream files
To display files you can use
fileURLtemplate helper or.link()method ofFileCursor.Template:
Shared code:
import { Meteor } from 'meteor/meteor'; import { FilesCollection } from 'meteor/ostrio:files';const Images = new FilesCollection({collectionName: 'Images'}); const Videos = new FilesCollection({collectionName: 'Videos'});
if (Meteor.isServer) { // Upload sample files on server's startup: Meteor.startup(() => { Images.load('https://raw.githubusercontent.com/VeliovGroup/Meteor-Files/master/logo.png', { fileName: 'logo.png' }); Videos.load('http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_5mb.mp4', { fileName: 'Big-Buck-Bunny.mp4' }); });
Meteor.publish('files.images.all', function () { return Images.find().cursor; });
Meteor.publish('files.videos.all', function () { return Videos.find().cursor; }); } else { // Subscribe to file's collections on Client Meteor.subscribe('files.images.all'); Meteor.subscribe('files.videos.all'); }
Client's code:
Template.file.helpers({ imageFile() { return Images.findOne(); }, videoFile() { return Videos.findOne(); } });For more expressive example see Streaming demo app
Download button
Template:
Shared code:
import { Meteor } from 'meteor/meteor'; import { FilesCollection } from 'meteor/ostrio:files'; const Images = new FilesCollection({collectionName: 'Images'});if (Meteor.isServer) { // Load sample image into FilesCollection on server's startup: Meteor.startup(function () { Images.load('https://raw.githubusercontent.com/VeliovGroup/Meteor-Files/master/logo.png', { fileName: 'logo.png', meta: {} }); });
Meteor.publish('files.images.all', function () { return Images.find().cursor; }); } else { // Subscribe on the client Meteor.subscribe('files.images.all'); }
Client's code:
Template.file.helpers({ fileRef() { return Images.findOne(); } });For more expressive example see Download demo
FAQ:
- Where are files stored by default?: by default if
config.storagePathisn't passed into Constructor it's equals toassets/app/uploadsand relative to running script:
- a. On
developmentstage:yourDevAppDir/.meteor/local/build/programs/server. Note: All files will be removed as soon as your application rebuilds or you runmeteor reset. To keep your storage persistent during development use an absolute path outside of your project folder, e.g./datadirectory.- b. On
production:yourProdAppDir/programs/server. Note: If using MeteorUp (MUP), Docker volumes must to be added tomup.json, see MUP usage- Cordova usage and development: With support of community we do regular testing on virtual and real devices. To make sure
Meteor-Fileslibrary runs smoothly in Cordova environment — enable withCredentials; enable{allowQueryStringCookies: true}and{allowedOrigins: true}on bothClientandServer. For more details read Cookie's repository FAQ- How to pause/continue upload and get progress/speed/remaining time?: see Object returned from
insertmethod- When using any of
accountspackages - packageaccounts-basemust be explicitly added to.meteor/packagesaboveostrio:files- cURL/POST uploads - Take a look on POST-Example by @noris666
- In Safari (Mobile and Desktop) for
DDPchunk size is reduced by algorithm, due to error thrown if frame is too big. Limit simultaneous uploads to6is recommended for Safari. This issue should be fixed in Safari 11. Switching tohttptransport (which has no such issue) is recommended for Safari. See #458- Make sure you're using single domain for the Meteor app, and the same domain for hosting Meteor-Files endpoints, see #737 for details
- When proxying requests to Meteor-Files endpoint make sure protocol
http/1.1is used, see #742 for detailsAwards:
Get Support:
help wantedissues;
devbranch. Please, always give expressive description to your changes and additions.
We would like to thank everyone who support this project. Because of those guys this project can have 100% of our attention.