Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 3.37 KB

link.md

File metadata and controls

75 lines (56 loc) · 3.37 KB

Link; or get downloadable URL

Use fileUrl helper with Blaze to get downloadable URL to a file.

There are two options to get downloadable URL to the uploaded file using .link() method:

FilesCollection#link

Use .link() method of FileCursor instance to create downloadable link from file's plain object. To get an Object use FilesCollection#collection.findOne({}) of for example inside end event on the Client and onAfterUpload on the Server

FilesCollection#link(fileRef, version, URIBase); // [*Isomorphic*]
  • fileRef {Object} - Object returned from MongoDB collection or after upload
  • version {String|void 0} - [OPTIONAL] File's subversion name, default: original. If requested subversion isn't found, original will be returned
  • URIBase {String} - [OPTIONAL] base URI (domain), default: ROOT_URL or MOBILE_ROOT_URL on Cordova.
  • Returns {String} - Absolute URL to file

FileCursor#link

Use .link() method of FileCursor instance to create downloadable link from a cursor returned for example from FilesCollection#findOne({})

FileCursor#link(version, URIBase); // [*Isomorphic*]
  • version {String|void 0} - [OPTIONAL] File's subversion name, default: original. If requested subversion isn't found, original will be returned
  • URIBase {String} - [OPTIONAL] base URI (domain), default: ROOT_URL or MOBILE_ROOT_URL on Cordova.
  • Returns {String} - Full URL to file

Required fields:

import { FilesCollection } from 'meteor/ostrio:files';
const imagesCollection = new FilesCollection({collectionName: 'images'});

imagesCollection.findOne({}, {
  fields: {
    _id: 1,
    public: 1,
    versions: 1, // <-- only when versioning is used
    extension: 1,
    _downloadRoute: 1,
    _collectionName: 1
  }
});

Examples

import { FilesCollection } from 'meteor/ostrio:files';
const imagesCollection = new FilesCollection({collectionName: 'images'});

// Usage:
imagesCollection.collection.find({}).forEach(function (fileRef) {
  imagesCollection.link(fileRef);
});

imagesCollection.findOne({}).link();
// Get thumbnail subversion
imagesCollection.findOne({}).link('thumbnail');
// Equals to above
const fileRef = imagesCollection.collection.findOne({});
imagesCollection.link(fileRef);

// Change domain:
imagesCollection.link(fileRef, 'original', 'https://other-domain.com/');
// Relative path to domain:
imagesCollection.link(fileRef, 'original', '/');