使用流星上传和下载文件



我是Meteor和Web开发的新手,我想为我的meteor Web应用程序实现以下目标:

  • 用户可以单击按钮上传.zip文件(我希望将其存储到我的本地数据库中,因为我正在 localhost 中构建概念验证运行(。
  • 该文件与Mongodb集合的特定对象相关联。
  • 在与该对象关联的客户端页面上,可以单击按钮下载该.zip文件。

我找到的有关此的所有信息要么已弃用,要么涉及 collectionFS,要么过于具体/对于利基问题。事实证明,这比问题看起来更复杂!

我要寻找的是一个类似教程的指南,解释如何做到这一点,或者一些有助于为实现上述功能进行设置的代码片段。

编辑

我已经设法使用包上传文件 tomi:meteor-upload .然后,这将创建一个/.uploads文件夹,我的文件将上传到该文件夹。

当我尝试使用<a href="/.uploads/myfilename" download target="_blank">Download</a>从客户端下载它们时,我得到下载的文件已损坏!

我还尝试将它们上传到比/.uploads文件夹/public文件夹,但仍然遇到同样的问题。

在这里可以看到,但没有找到解决方案,即使我手动chmod 777我的文件,我也会遇到问题!

谢谢!

你可以试试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);
});

您可以使用file.pipe(fs.createWriteStream(saveTo));

而 saveTo 就是你上传的路径,例如:C:/media/,并尝试创建链接的路径示例:localhost:80/media/image-here.png使用这些方法将这些链接存储在数据库中,您可以使用 APACHE 进行文件托管。

最新更新