如何在 Meteor 中创建动态资产 [英] How to create dynamic assets in Meteor

查看:47
本文介绍了如何在 Meteor 中创建动态资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很容易.我想创建用户可以通过单击链接下载的简单文件.

I thought this would be easy. I want to create simple files the user can download by clicking a link.

把你想要的写到服务器assets/app文件夹中,然后生成一个简单的链接

Write what you want into the servers assets/app folder and then generate a simple link

<a href="/new.txt" download="yourNewFile.txt">Download> me!</a>

将文件写入 Meteor 的服务器端资产文件夹很容易.并且上面的下载链接将始终以您指定的名称下载文件.

Writing files into Meteor's server side asset folder is easy. And the download link above will always download a file with the name you specified.

您将在客户端的下载文件夹中获得一个 yourNewFile.txt.但是,不幸的是,它的内容不会是您在服务器上写的内容 (new.txt).

You will get a yourNewFile.txt in the client's download folder. But, unfortunately its content will not be what you wrote on the server (new.txt).

如果您的内容的名称最初不在公共文件夹中,Meteor 会下载其启动 html 页面作为内容的奇怪行为.我认为这是错误.... 将上述锚点放入默认 Meteor 项目并单击链接 .. 甚至不要创建公用文件夹.你会得到一个下载的文件,名字和你要求的一样...

Meteor has the strange behavior of downloading its startup html page as the content if the name of your content wasn't originally in the public folder. I think this is bug .... put the above anchor into a default Meteor project and click the link .. don't even create a public folder. You get a downloaded file with the name you asked for...

因此,如果您将存根放在公共文件夹中(您知道要创建的资产的名称),那么您可以动态创建它们.

So, if you put stubs in the public folder (you know the names of the assets you are going to create) then you can create them dynamically.

我不知道这些名字.有什么方法可以让 Meteor 使用我想要使用的新名称更新"其资产列表?

I don't know the names before hand. Is there any way to get Meteor to 'update' its assets list with the new names I want to use?

我知道有些软件包可以做到这一点.我想自己做上面的,真的不应该这么难.

I know there are packages that can do this. I'd like to just do it myself as above, really shouldn't be this hard.

推荐答案

public/ 文件夹专门用于静态资产.其内容由节点 http 服务器提供.

The public/ folder intended use is specifically for static assets. Its content is served by the node http server.

如果你想在服务器上动态生成资产,你可以依赖iron:router服务器端路由.

If you want to dynamically generate assets on the server, you can rely on iron:router server side routes.

这是一个简单的例子:

lib/router.js

Router.route("/dynamic-asset/:filename",function(){
  var filename = this.params.filename;
  this.response.setHeader("Content-Disposition",
    "attachment; filename=" + filename);
  this.response.end("Hello World !");
},{
  name: "dynamic-asset",
  where: "server"
});

在服务器端路由控制器中,您可以访问 this.response 这是一个标准节点 HTTP 响应实例,以使用正确的服务器生成的内容响应客户端.例如,您可以使用 URL 中的最终参数查询您的 Mongo 集合.

In server-side route controllers, you get access to this.response which is a standard node HTTP response instance to respond to the client with the correct server generated content. You can query your Mongo collections using the eventual parameters in the URL for example.

client/views/download/download.html

<template name="download">
  {{#linkTo route="dynamic-asset" target="_blank" download=""}}
    Download {{filename}}
  {{/linkTo}}
</template>

client/views/parent/parent.html

<template name="parent">
  {{> download filename="new.txt"}}
</template>

linkTo 块助手必须在路由参数可作为模板助手访问的上下文中调用.它将生成一个锚标记,其 href 设置为 Router.path(route, dataContext).这意味着,如果我们的服务器端路由 URL 是 /dynamic-asset/:filename,则具有可访问 filename 的数据上下文并设置为 "new.txt" 将生成此 URL:/dynamic-asset/new.txt.

The linkTo block helper must be called in a context where the route parameters are accessible as template helpers. It will generate an anchor tag having an href set to Router.path(route, dataContext). It means that if our server-side route URL is /dynamic-asset/:filename, having a data context where filename is accessible and set to "new.txt" will generate this URL : /dynamic-asset/new.txt.

在此示例中,由于模板调用语法,我们将 download 模板的当前数据上下文设置为 {filename: "new.txt"}.

In this example we set the current data context of the download template to {filename: "new.txt"} thanks to the template invocation syntax.

注意 target="_blank" 是必要的,以避免被重定向到当前选项卡内的动态资产 URL,并且必须设置 download HTML 属性以避免将链接视为浏览器应在新选项卡中打开的内容.download 属性值无关紧要,因为它的值将在服务器端被覆盖.

Note that target="_blank" is necessary to avoid being redirected to the dynamic asset URL inside the current tab, and the download HTML attribute must be set to avoid considering the link as something the browser should open inside a new tab. The download attribute value is irrelevant as it's value will be overriden server-side.

这篇关于如何在 Meteor 中创建动态资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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