Write Buffer
to FS and add record to Files collection. This function is asynchronous.
buffer
{Buffer} - File data asBuffer
opts
{Object} - Recommended properties:opts.fileName
{String} - File name with extension, likename.ext
opts.type
{String} - Mime-type, likeimage/png
opts.size
{Number} - File size in bytes, if not set file size will be calculated fromBuffer
opts.meta
{Object} - Object with custom meta-dataopts.userId
{String} - UserId, default nullopts.fileId
{String} - id, optional - if not set - Random.id() will be used
callback
{Function} - Triggered after file is written to FS. Witherror
, andfileRef
, wherefileRef
is a new record from DBproceedAfterUpload
{Boolean} - ProceedonAfterUpload
hook (if defined) afterBuffer
is written to FS- Returns {Files} - Current FilesCollection instance
import fs from 'fs';
import { FilesCollection } from 'meteor/ostrio:files';
const imagesCollection = new FilesCollection({collectionName: 'images'});
fs.readFile('/data/imgs/sample.png', (error, data) => {
if (error) {
throw error;
} else {
imagesCollection.write(data, {
fileName: 'sample.png',
fileId: 'abc123myId', //optional
type: 'image/png'
}, (writeError, fileRef) => {
if (writeError) {
throw writeError;
} else {
console.log(`${fileRef.name} is successfully saved to FS. _id: ${fileRef._id}`);
}
});
}
});