Reputation: 17332
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...
Upvotes: 1
Views: 768
Reputation: 1813
Also, if you are using FS.EventHandlers.insertFiles
and don't see the loading bar, create this function in your client code:
/**
* Replace for original FS.EventHandlers.insertFiles function.
*/
function cfsInsertFiles(collection, options) {
options = options || {};
var afterCallback = options.after;
var metadataCallback = options.metadata;
function insertFilesHandler(event) {
FS.Utility.eachFile(event, function (file) {
var f = new FS.File(file);
var maxChunk = 2097152;
FS.config.uploadChunkSize =
(f.original.size < 10 * maxChunk) ? f.original.size / 10 : maxChunk;
if (metadataCallback) {
FS.Utility.extend(f, metadataCallback(f));
}
collection.insert(f, afterCallback);
});
}
return insertFilesHandler;
}
And finally use it instead FS.EventHandlers.insertFiles
. For example:
Template.files.events({
'dropped .imageArea': cfsInsertFiles(Images, {
metadata: function (fileObj) {
return {
owner: Meteor.userId(),
foo: "bar"
};
},
after: function (error, fileObj) {
console.log("Inserted", fileObj.name);
}
})
});
Upvotes: 0
Reputation: 20226
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) {});
});
Upvotes: 1