Upload Saved Book Media

Learn how to add the files collection needed to upload book media.

We uploaded the book details in the last lesson. In this lesson, we’ll add the ability to upload media files attached to a book. A book should have a cover page and a digital copy as a pdf. We’ll use the ostrio:files package, which is fantastic for uploading files. We add the package to our project by running meteor add ostrio:files on the terminal.

Files collection

Installing the ostrio:files package adds a FilesCollection. Open the imports/api/files/files.js file. This is the file that handles the file upload from the client and server.

On line 6, a new FilesCollection is created. Some parameters are set, like the allowClientCode which enables the package to allow an upload from the client.

The FilesCollection runs on both the server and client. The onBeforeUpload function runs before the file starts uploading. A check is performed inside the onBeforeUpload function for the type of file we’re uploading and on the size of the file, which, in our example, is less than 10MB. This function returns true if the uploaded file is accepted or false. In this example, a valid file is one with an extension that ends in either pdf, png, jpg, or jpeg and is less than 10MB.

The ...