[英]How can I get image data from a server side perl program and display in a div using javascript
I have done a lot of searching and either the answer doesn't work or it's not a good solution. 我做了很多搜索,要么答案不起作用,要么这不是一个好的解决方案。 The solution of just putting a reference into the html like: 将引用放入html的解决方案如:
<img src="html://localhost/cgi-bin/fd.cgi"/>
Has two problems, one is the image will change and it'll get cached. 有两个问题,一个是图像会改变,它会被缓存。 I know that I can put a random parameter on the call and get around the problem, but the other problem is that the program runs for a few seconds. 我知道我可以在调用中放置一个随机参数并解决问题,但另一个问题是程序运行了几秒钟。 I use javascript, setInterval, to loop. 我使用javascript,setInterval来循环。 The perl program reads an image file and annotates it. perl程序读取一个图像文件并对其进行注释。 This take a while, 3 or 4 seconds and the display blanks out during that time, so I have two divs, one is hidden while the other is being displayed. 这需要一段时间,3或4秒,显示屏在此期间显示空白,因此我有两个div,一个是隐藏而另一个是显示。 The logic is that I'll load the hidden one with an image and when the load is complete, I'll hide the first and display the second, next pass, vice versa. 逻辑是我将加载隐藏的图像,当加载完成时,我将隐藏第一个并显示第二个,下一个通道,反之亦然。 There seems to be no event I can trap at "load completion". 似乎没有任何事件我可以陷入“装载完成”。 None I saw in the debugger at any rate. 没有我在调试器中看到的无论如何。
The ajax call brings in the data but I can't figure out how to display it. ajax调用会引入数据,但我无法弄清楚如何显示它。 I base64 encode it in the perl script. 我将base64编码在perl脚本中。 Here's the javascript. 这是javascript。
$.ajax({ type: 'GET',
url: 'http://localhost/cgi-bin/fdmap/image.pl',
success: function(data)
{
$('#img1').html('<img src="data:image/jpg;base64,'+data+'"');
},
error: function(data)
{
console.log("Error ");
return true;
},
}); });
html: HTML:
<div id="img1"></div>
<div class="hidden" id="img2"></div>
Here's the perl snippet 这是perl片段
binmode STDOUT;
print $cgi->header("image/jpg;base64");
open INP,"pcb.jpg";
my $it;
while(<INP>) {
my $in = $_;
$it.=$in;
}
my $encoded = encode_base64($it);
print $encoded;
I'm just using a small jpg file for testing. 我只是使用一个小的jpg文件进行测试。 Once I figure out how to display the image data I"ll generate the image dynamically. 一旦我弄清楚如何显示图像数据,我将动态生成图像。
When I run the perl script from the console I get the following (truncated) output. 当我从控制台运行perl脚本时,我得到以下(截断的)输出。
Content-Type: image/jpg;base64^M ^M /9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEP ERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4e Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAF9AZwDASIA 内容类型:image / JPG; BASE64 ^ M ^ M / 9J / 4AAQSkZJRgABAQEASABIAAD / 2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEP ERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7 / 2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4e Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7 / wAARCAF9AZwDASIA
If I look at the data received via the javascript debugger I see: 如果我查看通过javascript调试器收到的数据,我看到:
HTTP/1.1 200 OK Date: Sun, 05 May 2013 15:28:43 GMT Server: Apache/2.2.14 (Ubuntu) Keep-Alive: timeout=15, max=99 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: image/jpg;base64 HTTP / 1.1 200 OK日期:太阳,2013年5月5日15:28:43 GMT服务器:Apache / 2.2.14(Ubuntu)Keep-Alive:timeout = 15,max = 99连接:Keep-Alive Transfer-Encoding:chunked Content -Type:image / jpg; base64
And the data in the response looks the same as the data dumped on the console. 并且响应中的数据与控制台上转储的数据相同。
However nothing is displayed and there are no error messages to be seen. 但是没有显示任何内容,也没有任何错误消息可供查看。 Just a blank screen. 只是一个空白的屏幕。 I've been working on this for many hours. 我一直在研究这个问题好几个小时。 Thanks for any help you can give me. 感谢你给与我的帮助。 Oh and I could care less if it works in IE or not. 哦,如果它在IE中工作,我可以更少关心。
Only with quick scan, so i'm not sure, but - are you sure than 只有快速扫描,所以我不确定,但是 - 你确定比
$('#img1').html('<img src="data:image/jpg;base64,'+data+'"');
^
//not missing the tag-end?---+
because you get 因为你得到了
<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASAB....."
and need (IMHO) 和需要(恕我直言)
<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASAB....." />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.