Reputation: 43
how to save a PDF or image file into a collection with meteor I tried but it just saves the pdf link or picture.
I tried the code below but it fits rather in the Files collection binary digits. please I want to do is insert the file into the collection and not the link to the file.
'change input' : function(event,template){
var file = event.target.files;
if (!file) return;
var reader = new FileReader();
reader.onload = function(event){
var buffer = new Uint8Array(reader.result)
Meteor.call('saveFile', buffer);
}
reader.readAsArrayBuffer(file);
}
/*** server.js ***/
Files = new Mongo.Collection('files');
Meteor.methods({
'saveFile': function(buffer){
Files.insert({data:buffer})
}
});
Upvotes: 4
Views: 894
Reputation: 683
You can use CollectionFS with dropbox or Amazon S3 store like this:
Collections file:
var dropboxStore = new FS.Store.Dropbox("files", {
key: //your key here,
secret: //Your secret here,
token: // Access tokenhere. Don’t share your access token with anyone.
folder: FolderName, //optional, which folder (key prefix) to use
// The rest are generic store options supported by all storage adapters
// transformWrite: myTransformWriteFunction, //optional
// transformRead: myTransformReadFunction, //optional
// maxTries: 1 //optional, default 5
});
Images = new FS.Collection("images", {
// stores: [new FS.Store.FileSystem("images", {path:"../../../../../.uploads"})]
stores: [dropboxStore]
});
Images.allow({
insert: function () {
return true;
},
update: function () {
return true;
},
download: function () {
return true;
}
});
Client side: On file change event
FS.Utility.eachFile(event, function (file) {
var imgfile = event.target.files[0];
var img = new Image();
img.src = window.URL.createObjectURL(imgfile);
img.onload = function () {
Images.insert(file, function (err, fileObj) {
free_spinz_symbol.set(fileObj);
});
};
});
Upvotes: 4
Reputation: 937
It might not be the best solution for you if you're up-to-date with the meteor version, because the project is currently being deprecated but CollectionFS used to be a great solution for file storage. Nowdays, Meteor-Files seems to be a better choice to handle PDF/Images storage
Upvotes: 2