Search code examples
javascriptmeteorcollectionfs

Upload progress bar for small files in meteor CollectionFS


With the meteor-cfs-ui-package you can show a progress bar for uploading files. But this only works well für files, which are bigger then 2 MB. If the files are smaller, the bar just jumps from 0% to 100%.

Here I found a solution for that, which uses this code:

if(fsFile.original.size < (2097152)*10) {
  var chunkSize = fsFile.original.size / 10;
  FS.config.uploadChunkSize = chunkSize;
}
uploadFile(fsFile, fullFileName, projectId);

But where do I have to put that code?

I declare my stores like this:

Images = new FS.Collection("images", {
    stores: [
        new FS.Store.FileSystem("something", {
            transformWrite: function (fileObj, readStream, writeStream) {
                // do transformations
            }           
        })]
    });

The upload is done like this:

FS.Utility.eachFile(event, function (file) {
    var newFile = new FS.File(file);
    newFile.uploadedFrom = Meteor.userId();

    data = Images.insert(newFile, function (error, fileObject) {});
});

So I guess it is a stupid question, but I really don't see, where to put that code...


Solution

  • The upload happens on collection insert so you should be able to set the chunkSize right before that:

    FS.Utility.eachFile(event, function (file) {
        var newFile = new FS.File(file);
        newFile.uploadedFrom = Meteor.userId();
    
        var maxChunk = 2097152;
        FS.config.uploadChunkSize =
          ( newFile.original.size < 10*maxChunk ) ? newFile.original.size/10 : maxChunk;
    
        data = Images.insert(newFile, function (error, fileObject) {});
    });