There is a lot of stuff out there, for file uploads, but I just need the simple use case of letting the user browse to a file, click “upload” and then be able to process it on the server… I want to store it on my local server
I have a simple example working by doing:
common.js
YourFileCollection = new FS.Collection("yourFileCollection", {
stores: [new FS.Store.FileSystem("yourFileCollection", {path: "~/meteor_uploads"})]
});
YourFileCollection.allow({
insert: function (userId, doc) {
return true;
},
update: function (userId, doc) {
return true;
},
remove: function (userId, doc) {
return true;
},
download: function (userId, doc) {
return true;
}
});
template:
<input class="your-upload-class" type="file">
<table class="table table-condensed">
<tbody>
{{#each theFiles}}
<tr>
<td>{{uploadedAt}}</td><td>{{original.name}}</td><td><a
href="{{url}}">link</a></td><td>{{#if
isUploaded}}uploaded{{/if}}</td><td><button
id="deleteFileButton">delete</button></td>
</tr>
{{/each}}
</tbody>
</table>
server:
Meteor.publish("fileUploads", function () {
console.log("publishing fileUploads");
return YourFileCollection.find();
});
client
Meteor.subscribe("fileUploads");
Template.home.events({
'click #deleteFileButton ': function (event) {
console.log("deleteFile button ", this);
YourFileCollection.remove({_id:this._id});
},
'change .your-upload-class': function (event, template) {
console.log("uploading...")
FS.Utility.eachFile(event, function (file) {
console.log("each file...");
var yourFile = new FS.File(file);
yourFile.creatorId = 123; // todo
YourFileCollection.insert(yourFile, function (err, fileObj) {
console.log("callback for the insert, err: ", err);
if (!err) {
console.log("inserted without error");
}
else {
console.log("there was an error", err);
}
});
});
}
});
Template.home.helpers({
theFiles: function () {
return YourFileCollection.find();
}
});
So, hopefully this will help someone… and now I need some help… I’ve implemented my own login/logout functionality, just by setting a session variable… so, how can I protect my downloads so they cannot be accessed when the user is not logged in? I’m not familiar with how the allow/deny works, as you can see I have just returned true from all of them to get things working for now…
Also, it may be obvious to some, but I was confused for a while as to how I could delete my uploads… I decided to just try a .remove on my collection and it worked… that’s cool, but where is this explained in the documentation? I might be missing a good example or source of documentation on this topic.
Thanks.