文件上传和下载使用流星 [英] File upload and download using meteor

查看:165
本文介绍了文件上传和下载使用流星的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢流星和网络开发,我想要为我的流星网络应用程序实现以下目标:




  • 一个用户可以点击一个按钮上传一个.zip文件(当我在本地主机上构建概念证明运行时,我希望将它存储到本地数据库中。)

  • 该文件与Mongodb集合的特定对象相关联。

  • 在与该对象相关联的客户端页面上,可以单击按钮下载该.zip文件。 >


我发现的关于这一点的所有信息都被弃用,引用collectionFS,或者太具体/针对一个利基问题。这已经证明比问题似乎是!



我将要寻找的是一个教程式指南,说明如何执行此操作或一些代码片段,有助于设置实现上述功能。



编辑



使用包 tomi:meteor-upload 加载文件。
然后创建一个 /。uploads 文件夹,我的文件上传到。



当我尝试然后使用< a href =/。uploads / myfilename下载目标=_ blank>下载< / a> ,然后从客户端下载下载的文件已损坏!



我也尝试将它们上传到 / public 文件夹,而不是 /。uploads 文件夹,但仍然有相同的问题。



这是看到这里,但是没有找到解决方案,即使我手动chmod 777我的文件我得到了问题! / p>

谢谢!

解决方案

你可以尝试busboy https://github.com/mscdex/busboy




this.route('/ upload',{
其中:'server',
方法:'POST',
名称:'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('文件['+ 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 / ,并尝试创建ang Path的链接示例: localhost:80 / media / image-here.png 使用方法将这些链接存储在数据库中,您可以使用APACHE进行文件托管。


I am new to Meteor and web development in general, what I would like to achieve for my meteor web-app is the following:

  • A user can click on a button to upload a .zip file (I want this to get stored to my local database as I am building a proof-of-concept run in localhost).
  • the file gets associated with a particular object of a Mongodb Collection.
  • On the client page associated with that object one can click on a button to download that .zip file.

All the information I found regarding this either deprecated, referring to collectionFS, or too specific/for a niche problem.This has proven more complicated than the issue appears to be!

What i would be looking for is either a tutorial-like guide explaining how to do so or some code snippets that would help get set-up for achieving the mentioned functionality.

EDIT

I have managed to upload files using the package tomi:meteor-upload . This then creates a /.uploads folder where my files are uploaded to.

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

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

This is seen here, but no solution has been found, even if I chmod 777 my files manually I get the problem!

Thanks!

解决方案

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 the saveTo is the path you where upload, example: C:/media/ , and try to create ang Path of the link example: localhost:80/media/image-here.png use method to store these link in your database, you can use APACHE for File hosting.

这篇关于文件上传和下载使用流星的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆