jin123456bat jin123456bat
关注数: 16 粉丝数: 183 发帖数: 6,110 关注贴吧数: 44
nodejs版本 v0.10.15,用createReadStream始终没法下载文件,求… 尝试过各种办法,百度都翻了好几篇了。,还是没解决代码如下。 filePath = "D:/web/4.jpg"; function readFile(filePath,response) { filePath = path.join(filePath); var ContentType; if(filePath.lastIndexOf(".") == -1) throw new Error("Directory is not support download!"); var fileType = filePath.substring(filePath.lastIndexOf(".")+1);//文件类型 ContentType = config.MIME_TYPES[fileType]||"application/octet-stream"; fs.exists(filePath,function(exists) { if (exists) { var rOption = {flags : 'r',encoding : null,fd: null,mode : 0666,autoClose: true} if(config.DEBUG)console.log("["+core.time()+"] GET files:"+filePath); var ReadStream = fs.createReadStream(filePath,rOption); ReadStream.on('data',function(data){response.write(data);}); //ReadStream.on("data",function(data) //{ //response.write(data); //}); ReadStream.on("error", function() { if(config.DEBUG)console.log("["+core.time()+"] read files:"+filePath+" has an error!"); response.writeHead(404,{"Content-Type": "text/plain"}); response.end(); }); response.writeHead(200, {"Content-Type": ContentType}); ReadStream.pipe(response); //response.write(filePath); ReadStream.on("end", function() { //数据发送完毕 if(config.DEBUG)console.log("["+core.time()+"] read files:"+filePath+" success!"); response.end(); }); }else{ if(config.DEBUG)console.log("["+core.time()+"] files:"+filePath+" not exist!"); response.writeHead(404,{"Content-Type": "text/plain"}); response.end(); } }); } 假如按上面那种方式,浏览器一片空白。没有任何图片下载或显示假如将ReadStream.pipe(response);注释掉将//ReadStream.on("data",function(data)//{ //response.write(data);//});解除注释,可以触发end方法。我确定文件存在的。因为console.log("["+core.time()+"] GET files:"+filePath);这句可以在cmd中显示出来response.write(data);始终返回的null是不是这个版本的api有问题阿。为什么一直都不行呢?还是说有其他的问题?
吐槽!吐槽!吐槽!吧里有木有linux大神,小弟求教导... 接了一个关于云计算的活.要用到Hbase..无奈由原来的windows转到现在的ubuntu. 这个破ubuntu坑死爹了..学校教过linux,不过一直在菜鸟阶段 各种配置,各种搜索引擎基本都翻来覆去的...好吧.还算顺利.花了好几天终于是搞定了. 但是还是有写莫名其妙的错误 让我的蛋蛋疼阿!! 其他的 不说了.截图给大家这是用apache2下的页面信息.GB2312貌似没什么问题..但是我明明在配置文件中添加了 header("Content-Type: text/html; charset=UTF-8"); 为什么还是2312? 浏览器的地址是没有问题的阿 http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Flocalhost%2Fscore%2F&urlrefer=8773b356f51c487954bb4b0a4ab852d6 这都对的啊~~好吧2312就2312大不了改编码呗..但是我无意中进入到了这个页面 http://tieba.baidu.com/mo/q/checkurl?url=http%3A%2F%2Flocalhost%2Fscore%2Findex.php&urlrefer=08b6854c10b2d52778c17f9b308c4bd3这回居然又是utf8了..这怎么搞的..我就擦了阿..有木有大神能教教我?apache默认进入的难道不是index.php了?可是我目录下也没有其他php文件了阿., 这还不算蛋疼的.更蛋疼的是他报错大家看好了.Hbase.class.php文件的第4行错误.,我立马跳转过去.. 结果.那第4行哪里错误了?阿? 怎么就错了?阿? 这不坑死我吗? 哎.说多了都是眼泪. 顺便吐槽下百度真TM垃圾.ubuntu下默认浏览器firefox居然差点进不去百度贴吧.我用的ibus发帖框里居然打字老自动选择第一个字.这写字都是在gedit下写完然后在复制进去的..我了个擦阿..哭死!!!! 吐槽!!! 吐槽!!! 百度你就是个垃圾!!!
首页 1 2 下一页