实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
来源:互联网 发布:ac68u对访客网络限速 编辑:程序博客网 时间:2024/05/18 07:28
接这篇博客。http://blog.csdn.net/a6383277/article/details/23023269
上篇博客的下载部分我将讲的有点简陋,没有涉及到文件流的读取,因此补充如下。[这个解决方案也不完整的,以后出来完整方案我会继续补充]
项目仅需要一个server文件夹下的index.js文件即可。当然也都需要iron-router包
代码如下:
var fs = Npm.require('fs');/* 暂时无法通过异步回调函数进行 原因:https://github.com/EventedMind/iron-router/issues/300 var displayImag = function(hashCode,response){//如果文件是存硬盘,hashCode应该避免访问其他资源,或者采取其他非字符串拼接方式var filePath = "/home/ec/@hashCode@.png".replace('@hashCode@',hashCode.replace(/(\.)+|(\/)/g,""));fs.exists(filePath,function(exists){if(!exists){response.writeHead(404,{"Content-Type":"text/plain"});response.write("404,请求资源"+uri+"不存在");response.end();return;}var fileStream = fs.createReadStream(filePath);response.setHeader("Content-Disposition", "inline;");response.writeHead(200, {"Content-Type": "image/png"});fileStream.pipe(response);fileStream.on('end',function(){response.end();}) });}*/Router.map(function() {this.route('postsShow', { where: 'server',path: '/img/:hashCode', action: function() { var hashCode = this.params.hashCode; console.log(hashCode) //displayImag(hashCode,this.response) //如果文件是存硬盘,hashCode应该避免访问其他资源,或者采取其他非字符串拼接方式 var filePath = "/home/ec/@hashCode@.png".replace('@hashCode@',hashCode.replace(/(\.)+|(\/)/g,"")); var fileSize = fs.statSync(filePath)this.response.writeHead(200, { 'Content-Type': 'image/png', // Change to the type of file you're serving 'Content-Disposition': 'inline', 'Content-Size': fileSize})this.response.write(fs.readFileSync(filePath))this.response.end() }});});
由于iron-router使用了Connect中间件,使得无法文件流通过异步读取调用来完成文件下载,我会持续关注这个问题。
0 0
- 实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
- 实时开发框架Meteor 实际应用系列<一>---文件的上传和下载
- 实时开发框架Meteor API解读系列<一> Meteor.methods
- 实时开发框架Meteor基础入门系列<一>--安装与HelloWorld
- 实时开发框架Meteor基础入门系列<零>--简介
- 实时开发框架Meteor API解读系列<二>Core
- 实时开发框架Meteor API解读系列<四>Server connections
- 实时开发框架Meteor API解读系列<五>Session
- 实时开发框架Meteor API解读系列<六> DDP
- 实时开发框架Meteor API解读系列<七> Collection --01
- 实时开发框架Meteor API解读系列<八> Timers
- 实时开发框架Meteor API解读系列<三>Publish and Subscribe-(1)
- 实时开发框架Meteor API解读系列<三>Publish and Subscribe-(2)
- 文件的上传和下载(一)
- 文件的上传和下载(一)
- Meteor开发框架为你带来下一代高品质实时Web App开发的新模式
- 惊艳的基于nodejs的实时Web开发框架Meteor介绍
- 实时Javascript开发框架Clouda、Meteor、Firebase对比
- 优化临时表,使数据库性能大幅提升
- 第六周作业
- 赶着学的第一篇
- MySQL——索引与优化
- (转载)ArcGIS Engine中各种点的创建方法
- 实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
- java checkstyle检查问题
- 研究称施展“微笑魔法”可使人看起来更聪明
- activity详解
- libgdx 自适应分辨率
- 已打捞出一些物品并发现油迹
- 【转】intent.putextra的用法
- java UDP协议程序设计
- [Python]代码重复率查找工具Clone Digger