简体   繁体   English

无法通过phantomjs webserver传递图像文件

[英]Can not deliver image files through phantomjs webserver

i am trying tp get phantomjs webserver works for me 我正在尝试获取phantomjs webserver为我工作

I want to serve 2 files, html file , and a png image file, the html file is served well and rendered correctly in the browser, but the png file is not 我想提供2个文件,html文件和一个png图像文件,html文件很好地提供并在浏览器中正确呈现,但是png文件不是

here is the code for the server 这是服务器的代码

var fs = require('fs');
function loadFile(name){
if(fs.exists(name)){
    console.log(name+ " File  exist");
    return fs.open(name,"r");
}else {
    console.log("File do not exist");
}
}
var server, service;

server = require('webserver').create();

service = server.listen(8080, function (request, response) {    

if(request.url.split(".")[1] === "html" ){
    var fi = loadFile("./test.html");
    response.statusCode = 200;
    response.write(fi.read());
    fi.close();
    response.close();
}else if (request.url.split(".")[1] === "png"){
    var fi = loadFile("./output_87.png");
    response.headers = {"Content-Type":"image/png"};
    response.statusCode = 200;
    response.write(fi.read());
    fi.close();
    response.close();
}
});

and this is the html file markup 这是html文件标记

<html>
<head><title>title</title></head>
<body><h1> Hello world </h1>
    <img src="output_87.png" alt="image">
</body>
</html>

when viewing this file in the browser, the png file is not rendered, and even if i tried to point the browser to the png file, it does not render it 在浏览器中查看此文件时,不会呈现png文件,即使我尝试将浏览器指向png文件,也不会呈现它

i checked with the chrome developer tools the network status and it confirm that the file is fully downloaded by the browser 我使用chrome开发人员工具检查了网络状态,并确认浏览器已完全下载该文件

what is wrong? 怎么了?

by the way, i have to use phantomjs, please d not tell me to use another server 顺便说一句,我必须使用phantomjs,请不要告诉我使用另一台服务器

thanks 谢谢

Joe

This works for me (assuming you have the response object): 这对我有用(假设你有响应对象):

  var fs = require("fs");
  var image = fs.open("image.png", "rb");
  var data = image.read();
  image.close();
  response.setHeader("Content-Type", "image/png");
  response.setEncoding("binary");
  response.write(data);
  response.close();

I believe your fs.open() call should use "rb" for binary read mode if you're reading a PNG file. 我相信如果您正在读取PNG文件,您的fs.open()调用应该使用“rb”进行二进制读取模式。 Mode "r" works for your html because it's text, but while it will still read the PNG file and serve it to the browser, the image data is not renderable. 模式“r”适用于您的html,因为它是文本,但是当它仍然会读取PNG文件并将其提供给浏览器时,图像数据不可渲染。

You're trying to use asynchronous functions as if they were synchronous. 您正在尝试使用异步函数,就好像它们是同步的一样。 All the filesystem functions you're using ( exists() , open() , and read() ) are asynchronous and try to call a callback (which you're not supplying) with the desired result; 您正在使用的所有文件系统函数( exists()open()read() )都是异步的,并尝试使用所需的结果调用回调(您没有提供)。 they don't return it. 他们不return Either use the sync versions of those functions or (far better) learn how to use asynchronous I/O. 要么使用这些函数的同步版本,要么(更好)学习如何使用异步I / O.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM