admin管理员组文章数量:1395198
i am trying tp get phantomjs webserver works for me
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
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>
<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
i checked with the chrome developer tools the network status and it confirm that the file is fully downloaded by the browser
what is wrong?
by the way, i have to use phantomjs, please d not tell me to use another server
thanks
Joe
i am trying tp get phantomjs webserver works for me
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
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>
<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
i checked with the chrome developer tools the network status and it confirm that the file is fully downloaded by the browser
what is wrong?
by the way, i have to use phantomjs, please d not tell me to use another server
thanks
Joe
Share Improve this question asked Jun 19, 2012 at 22:36 JosephJoseph 3,1654 gold badges25 silver badges33 bronze badges 1- 1 I've just sent a PR on phantomjs repository to fix this issue github./ariya/phantomjs/pull/288/files (code is probably crappy, but it works for me) – NiKo Commented Jul 1, 2012 at 21:13
3 Answers
Reset to default 7This 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. 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.
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; they don't return
it. Either use the sync versions of those functions or (far better) learn how to use asynchronous I/O.
本文标签: javascriptCan not deliver image files through phantomjs webserverStack Overflow
版权声明:本文标题:javascript - Can not deliver image files through phantomjs webserver - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744586320a2614217.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论