博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实时开发框架Meteor 实际应用系列<一>---文件的上传和下载[补充]
阅读量:2201 次
发布时间:2019-05-03

本文共 1680 字,大约阅读时间需要 5 分钟。

接这篇博客。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中间件,使得无法文件流通过异步读取调用来完成文件下载,我会持续关注这个问题。

你可能感兴趣的文章
使聊天机器人具有个性
查看>>
使聊天机器人的对话更有营养
查看>>
一个 tflearn 情感分析小例子
查看>>
attention 机制入门
查看>>
手把手用 IntelliJ IDEA 和 SBT 创建 scala 项目
查看>>
GAN 的 keras 实现
查看>>
AI 在 marketing 上的应用
查看>>
Logistic regression 为什么用 sigmoid ?
查看>>
Logistic Regression 为什么用极大似然函数
查看>>
SVM 的核函数选择和调参
查看>>
LightGBM 如何调参
查看>>
用 TensorFlow.js 在浏览器中训练神经网络
查看>>
cs230 深度学习 Lecture 2 编程作业: Logistic Regression with a Neural Network mindset
查看>>
梯度消失问题与如何选择激活函数
查看>>
为什么需要 Mini-batch 梯度下降,及 TensorFlow 应用举例
查看>>
为什么在优化算法中使用指数加权平均
查看>>
什么是 Q-learning
查看>>
用一个小游戏入门深度强化学习
查看>>
5 分钟入门 Google 最强NLP模型:BERT
查看>>
初探Java设计模式4:一文带你掌握JDK中的设计模式
查看>>