Meteor 前端 RESTful API 通过后端 API 下载文件
来源:互联网 发布:c语言 map 编辑:程序博客网 时间:2024/06/05 21:08
Meteor 下载文件
问题场景
后端 HTTP 服务器提供一个下载接口,但是需要前端 Meteor 能够给浏览器用户开一个URL来下载这个文件。
举例:在线的Meteor Logo文件就好比后端提供的 RESTful API,然后我们给浏览器客户暴露一个 URL 来下载
Meteor 依赖
安装所有依赖:
meteor add httpmeteor add cfs:http-methodsmeteor add froatsnook:request
说明:
* cfs:http-methods * 用来给Meteor项目提供一个方便创建 RESTful API 的机会,非常方便。
here for details.
* froatsnook:request * 用来给Meteor项目提供一个便利访问二进制数据的 RESTful API 的机会,也是非常简洁方便,支持同步请求。
here for details.
示例代码
Meteor 服务器端
if (Meteor.isServer) { // exports a RESTful API for browser HTTP.methods({ // name RESTful API as "GET /download-meteor-logo" '/download-meteor-logo': function() { // A file in streaming, so need to response to browser as a streaming. var res = this.createWriteStream(); // Play as a HTTP client for requesting image. // It is Sync way var result = request.getSync("https://meteor.com/meteor-logo.png", { encoding: null }); var buffer = result.body; // TODO: need to set header for response here which transfered by // response of logo request. res.write(buffer); res.end(); } });} // Meteor.isServer enclosure
浏览器查看是否有Logo
- 首先得启动 Meteor 服务
meteor --port 3000
- 打开浏览器访问 http://localhost:3000/download-meteor-logo
完
1 0
- Meteor 前端 RESTful API 通过后端 API 下载文件
- 使用Flask搭建Restful API后端服务器
- RESTful API实战笔记(接口设计及Java后端实现)
- RestFul API
- RESTful API
- RESTful API
- Restful API
- RESTful API
- RESTful API
- RESTful API
- RESTful API
- restful api
- RESTful API
- Restful API
- Restful API
- RESTful API
- RESTful API
- RESTful API
- 状态改变位置量
- IOS 开发,调用打电话,发短信,打开网址
- 【持久层】数据库事务基础——事务的隔离级别
- JSON概述
- Ubuntu 14.04 Eclipse JNI环境搭建(一)之生成JNI头文件
- Meteor 前端 RESTful API 通过后端 API 下载文件
- 预编译布局
- Kafka 学习笔记(十)之consumer写入file
- Java中反射机制和Class.forName、实例对象.class(属性)、实例对象getClass()的区别
- 动态代理工具类
- jsp中textarea中输入文本存入数据库,然后按照输入的格式显示
- 字符串替换
- Error 0xc000007b while Running SqlTools
- [Leetcode 22, Medium] Generate Parentheses