How do you actually get the contents of a file using Meteor-Files?

Hi, I’m trying to integrate Meteor-Files into my Meteor/React app, but I’m not quite sure on how to actually get the contents of a file. I used the same example as presented in the Readme, except I defined an absolute storagePath.

import { Meteor }          from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';

const Images = new FilesCollection({
  debug: true,
  collectionName: 'Images',
  storagePath: "/home/XXX/XXX/testDir"
});

// To have sample image in DB we will upload it on server startup:
if (Meteor.isServer) {
  Images.denyClient();
  Images.collection.attachSchema(Images.schema);

  Meteor.startup(function () {
    if (!Images.find().count()) {
      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 {
  Meteor.subscribe('files.images.all');
}

export default Images;
  1. On my client in React, I import Images but how am I supposed to get the actual file contents? I want to display the image but I can’t use the path stored in the Images collection because Meteor parses the path through the public/ folder.

  2. Similarly, if this is a text file or any other file, how do I get the contents of the file? Say if I want to get the contents of a text file and display it in a React component, how should this be done?

I couldn’t find any instance in the documentation which illustrates how this should be done specifically.

I’m trying to be able to console.log the data first before actually using it in my component. I tried to use link() but I don’t think this is the correct way of doing it as it throws some errors:

const Component = () => {
    useEffect(() => {
        console.log(Images.link())
    }, [])
    return <div />
}

Appreciate any help, thanks.

Resolved in this thread. I had to run it exclusively on the server in order to get it working, not in React.

1 Like