(注意:[修改于2014.3.30]由于前天的Meteor进行版本升级到0.8.0版本,导致router包无法正常使用,需到等待包作者的更新。因此这篇博客在router包作者未更新到相应版本前,是无法正常使用的,仅作为参考!
[修改于4.14]该包作者已经推荐 使用iron-router作为router功能。请移步我的另外一篇博客:http://blog.csdn.net/a6383277/article/details/23023269
)
话不多说。
分两种情况。一种使用Linux,一种使用windows。
用Linux开发meteor是简单易用的。windows相对来说比较麻烦。
先上Linux的步骤。
第一 ,你需要安装第三方 meteor包管理器 meteorite。meteorite 官方文档在这里·[https://github.com/oortcloud/meteorite/]
安装meteorite非常简单:
使用
$ npm install -g meteorite
即可。可能需要管理权限安装。
安装好后,就可以使用mrt 命令来代替meteor命令了。mrt的具体使用请查询官方文档。不在这里描述了。
在自己的工作目录运行 一下命令:
mrt create testDownload
cd testDownload
mrt add router
这样就弄好了一个路由功能。
完成之后,删除 testDownload 目录下的 .js .css .html 这三个自动生成的文件。
新建一个server文件夹。里面新建一个js 文件,内容如下:
var fs = Npm.require("fs");
Meteor.Router.add( '/report', 'GET', function () {
return [200,
{
'Content-type': 'text/html',
'Content-Disposition': "attachment; filename=a.html"
}, fs.readFileSync("/home/ec/a.html")];
} );
其中
Content-type
Content-Disposition
之类的东西不再解释,这个是http 基础协议的常识,不了解请自行百度或google。
做后既可以了。
然后在testDownload 下u运行
meteor
或者
mrt
都可以 启动meteor
然后在浏览器端输入 localhost::3000/report 即可下载文件。
有管Router的用法,请自行查询官方文档:https://github.com/tmeasday/meteor-router
以上内容 参考了 SO上的答案,原文见:http://stackoverflow.com/questions/17038381/how-to-handle-conditional-file-downloads-in-meteor-js
补上window实现部分。
window的麻烦之处在于 meteorite 不支持windows,但是没有关系。
请下载 http://download.csdn.net/detail/a6383277/6658427 我弄过来的包,这个已经把相关依赖弄好了。
首先 在你i自己的工作目录下 创建meteor工程。步骤和linux差不多:
meteor create testDownload
进入到testDownload目录下。把下载的文件解压。解压得到的一个packages文件夹直接放在testDownload目录下。
打开testDownload 下的 .meteor文件夹。 找到文件packages文件,编辑在最后一行加上router
最后应该类似于这样:
# Meteor packages used by this project, one per line.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.
standard-app-packages
autopublish
insecure
preserve-inputs
router
既可以。然后就是新建server文件夹,创建js文件,复制粘贴代码,运行meteor。和linux下面的目录差不多。最后访问浏览器localhost:3000/report
搞定收工。