JMA
JMA

Reputation: 994

Where can I get the uploaded file in Meteor Files?

I am using Meteor and React JS. I also added Meteor Files.

https://github.com/VeliovGroup/Meteor-Files

By using this code,

this.Images = new FilesCollection({collectionName: 'Images'});
export default class Logo extends TrackerReact(React.Component){
    constructor(){
    ...
    Meteor.subscribe('files.images.all');
    }
uploadLogo(e){
    if (e.currentTarget.files && e.currentTarget.files[0]) {
      // We upload only one file, in case 
      // there was multiple files selected
      var file = e.currentTarget.files[0];
      if (file) {
        var uploadInstance = Images.insert({
          file: file,
          streams: 'dynamic',
          chunkSize: 'dynamic',
          transport: 'http'
        }, false);

        uploadInstance.on('start', function() {
          //template.currentUpload.set(this);
        });

        uploadInstance.on('end', function(error, fileObj) {
          if (error) {
            alert('Error during upload: ' + error.reason);
          } else {
              console.log("done");
            alert('File "' + fileObj.name + '" successfully uploaded');
          }
        });
        uploadInstance.start();
      }

    }else{
        console.log("error");
    }
}
render(){
...
<input type="file" id="fileinput" onChange={this.uploadLogo.bind(this)} />
}

I am able to upload the file but I don't see any files in my directory. Here is my publish.js,

this.Images = new Meteor.Files({
  debug: true,
  collectionName: 'Images',
  allowClientCode: false, // Disallow remove files from Client
  onBeforeUpload: function (file) {
    // Allow upload files under 10MB, and only in png/jpg/jpeg formats
    if (file.size <= 1024*1024*10 && /png|jpg|jpeg/i.test(file.extension)) {
      return true;
    } else {
      return 'Please upload image, with size equal or less than 10MB';
    }
  }
});
Meteor.publish('files.images.all', function () {
    return Images.find().cursor;
  });

How can I display the image? How can I limit the user to upload files that are only images?

For me, their API Docs is not rich. I can't understand what the things they are talking in their docs.

Upvotes: 0

Views: 476

Answers (1)

Mikkel
Mikkel

Reputation: 7777

By default uploaded files are stored in the file system. Read the FAQ:

Where are files stored by default?: by default if config.storagePath isn't passed into Constructor it's equals to assets/app/uploads and relative to running script:

  • On development stage: yourDevAppDir/.meteor/local/build/programs/server

Note: All files will be removed as soon as your application rebuilds or you run meteor reset. To keep your storage persistent during development use an absolute path outside of your project folder, e.g. /data directory.

  • On production: yourProdAppDir/programs/server

So you need to set a location with config.storagePath

Upvotes: 2

Related Questions