https://github.com/davebryson/meteor_file_upload_example
以上是使用 Meteor.router 的原始代码
我想只使用 Iron-router 转换此代码,而不是以前的路由器包
但问题是当我上传文件时,
我不明白如何使用 Iron-router api 转换这些代码。
我认为 index.html 和服务器端 main.js 代码存在问题,但我无法解决。
你能用铁路由器转换下面的代码吗?
在原始代码中的服务器端 main.js 中。
Meteor.Router.configure(
{
bodyParser:
{
uploadDir: 'uploads',
hash: 'sha1',
keepExtensions : true
}
}
);
Meteor.Router.add('/upload/file', 'POST',
function()
{
var title = this.request.body.fileTitle,
size = this.request.files.fileInfo.size,
path = this.request.files.fileInfo.path,
name = this.request.files.fileInfo.name,
obj = {title: title, size: size, path: path, name: name};
Files.insert(obj);
// redirect to root
return [301, {Location: '/'}, ""]
}
);
我已经转换了客户端 main.js 中的代码,如下所示
Router.map(function () {
this.route('listFiles', {path: '/'});
this.route('addFile', {path: '/add'});
});
Template.listFiles.helpers({
list: function () {
return Files.find({});
}
});
Template.listFiles.events({
'click #addFileBtn' : function(e) {
e.preventDefault();
Router.go('/add');
}
});
Template.addFile.events({
'click #cancelBtn': function(e){
e.preventDefault();
Router.go('/');
}
});
Meteor.subscribe("files");
关键是我无法通过 Iron-router 处理如何在服务器端代码中使用 http 方法