Upload and download files using a meteor

I am new to Meteor and web development in general, what I would like to achieve for my meteor web application:

  • The user can click on the button to download the .zip file (I want this to be saved in my local database, since I am creating a trial version of the evidence concept in localhost).
  • the file is attached to a specific object of the Mongodb collection.
  • On the client page associated with this object, you can click the button to download this .zip file.

All the information I found regarding this is either outdated referring to collectionFS, or is too specific / for the niche problem. This turned out to be more complicated than the problem.

What I would be looking for is a tutorial explaining how to do this, or some code snippets that will help you get the setup to achieve this functionality.

EDIT

I was able to upload files using the tomi: meteor-upload package. Then the /.uploads folder is created where my files are uploaded.

When I try to download them from the client, but using <a href="/.uploads/myfilename" download target="_blank">Download</a> , I get that the downloaded files are damaged!

I also tried uploading them to the /public folder than to the /.uploads folder, but still having the same problem.

It is visible here , but no solution was found, even if I chmod 777 my files manually, I get a problem!

Thanks!

0
source share
1 answer

You can try busboy https://github.com/mscdex/busboy :

 this.route('/upload', { where: 'server', method: 'POST', name:'upload', onBeforeAction: (function (req, res, next) { //busboy code here var busboy = new Busboy({ headers: req.headers }); busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); file.on('data', function(data) { console.log('File [' + fieldname + '] got ' + data.length + ' bytes'); }); file.on('end', function() { console.log('File [' + fieldname + '] Finished'); }); }); busboy.on('field', function(fieldname, encoding, mimetype) { console.log('Field [' + fieldname + ']: value: ' + inspect(val)); }); busboy.on('finish', function() { console.log('Done parsing form!'); res.writeHead(303, { Connection: 'close', Location: '/' }); res.end(); next(); }); req.pipe(busboy); }); 

you can use file.pipe(fs.createWriteStream(saveTo));

and saveTo is the path where you download, for example: C:/media/ , and try to create an ang Path for the link example: localhost:80/media/image-here.png use the method to store this link in your database, you can Use APACHE to host files.

0
source

All Articles