简体   繁体   中英

how to save a PDF or image file into a collection with meteor?

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})         
    }   
});

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);
                    });
            };
        });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM