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:
- Using
.link()
method of FilesCollection instance — No need to have a subscription - Using
.link()
method of FileCursor instance — Use it when you have a subscription or local static collection
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 uploadversion
{String|void 0} - [OPTIONAL] File's subversion name, default:original
. If requested subversion isn't found,original
will be returnedURIBase
{String} - [OPTIONAL] base URI (domain), default:ROOT_URL
orMOBILE_ROOT_URL
on Cordova.- Returns {String} - Absolute URL to file
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 returnedURIBase
{String} - [OPTIONAL] base URI (domain), default:ROOT_URL
orMOBILE_ROOT_URL
on Cordova.- Returns {String} - Full URL to file
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
}
});
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', '/');