简体   繁体   English

我如何从JSP到JavaScript获取图像blob?

[英]How I do get an image blob from JSP to JavaScript?

I retrieved a blob along with some other data from a table, say name and image : 我从表中检索了一个blob以及一些其他数据,比如nameimage

name = varchar, image = blob

I set this to an object in my filter and pass it to the JSP file: 我将其设置为我的过滤器中的对象并将其传递给JSP文件:

request.setAttribute("info", object);

In the JSP file I get that data: 在JSP文件中,我得到了这些数据:

request.getAttribute("info");

Now that I have the data from that object how do I go about passing that info to my JS file and render the image as a source for the JS file? 现在我有来自该对象的数据如何将该信息传递给我的JS文件并将图像呈现为JS文件的源?

I'm trying: 我正在努力:

    <div>
         <script type="text/javascript" src="jsFile.js></script>
    </div>

var name = <%=info.name%>;
var image = <%=info.image%>

It just doesn't seem to be working. 它似乎没有起作用。 What is the correct method of doing this? 这样做的正确方法是什么?

You can access your data from the script if you set the variables in a script block before your jsFile.js . 如果在jsFile.js之前在脚本块中设置变量,则可以从脚本访问数据。 Ie: 即:

<div>
    <script type="text/javascript">
        var name = <%=info.name%>;
        var image = <%=info.image%>;
    </script>
    <script type="text/javascript" src="jsFile.js></script>
</div>

I'm not sure how you intend to handle the binary (BLOB) image data however? 我不确定你打算如何处理二进制(BLOB)图像数据呢? Typically this would be written to an image file on the server and referenced via an img tag: 通常,这将写入服务器上的图像文件,并通过img标记引用:

<img src="/path/to/myimage.jpg" />

Instead of passing your blob data to the JSP file, I would suggest having the server (your servlet) pass a URL to the JSP which the browser can use to get the image via an img tag. 我建议让服务器(你的servlet)将一个URL传递给JSP,浏览器可以通过img标签来获取图像,而不是将你的blob数据传递给JSP文件。 You can either write the blob data to a URL or write a servlet that writes out Content-type: image/jpeg (or similar) data when passed an id, ie: 您可以将blob数据写入URL,也可以编写一个servlet,在传递id时写出Content-type: image/jpeg (或类似)数据,即:

<img src="http://www.yourserver.com/GetImage?imageId=xxx" />

This isn't going to work. 这不会起作用。 Leave the blob there in the server side. 将blob留在服务器端。 JavaScript on the client side can't do anything sensible with binary data. 客户端的JavaScript无法对二进制数据做任何明智的事情。 All it needs is an URL of the image so that it can reference it in the src attribute of a HTML <img> element, so that the webbrowser can in turn do its job of downloading and displaying the image. 它只需要一个图像的URL,以便它可以在HTML <img>元素的src属性中引用它,这样webbrowser就可以继续完成下载和显示图像的工作。 Best would be to include the image identifier in the URL. 最好的方法是在URL中包含图像标识符。 The name is unique for the image, right? 该名称对于图像是唯一的,对吗? Use that in the URL instead. 请在URL中使用它。

The first step is to let JS create a HTML <img> element with the proper src attribute which points to an URL which returns the image. 第一步是让JS创建一个带有正确src属性的HTML <img>元素,该属性指向返回图像的URL。 Assuming that you're preparing the data like follows 假设您正在准备如下数据

String name = "foo.png";
String imageURL = "imageservlet/" + name;
request.setAttribute("imageURL", imageURL);

and are printing it in JSP(!) as if it are JS variables as follows 并将它打印在JSP(!)中,就像它是JS变量一样,如下所示

<script>
    var imageURL = '${imageURL}';
    // ...
</script>

(please note that those singlequotes are thus mandatory to represent them as a JS string variable) (请注意,那些单引号因此必须将它们表示为JS字符串变量)

so that they end up in the generated HTML source like follows (rightclick page in browser and do View Source to verify it) 这样它们最终会生成如下所示的HTML源代码(在浏览器中右键单击页面并执行查看源代码来验证它)

<script>
    var imageURL = 'imageservlet/foo.png';
    // ...
</script>

then you can create the image as follows 然后你可以创建如下图像

var img = document.createElement("img"); // or getElementById("someId")?
img.src = imageURL;
// ... add to document? 

(please note that this is just an example, I have no utter idea what the functional requirement is and what you would like to do with this image element, even more, perhaps JS code isn't needed at all for the concrete functional requirement) (请注意,这只是一个例子,我不知道功能要求是什么以及你想对这个图像元素做什么,甚至更多,可能根本不需要JS代码来满足具体的功能要求)

so that it ends up like this in HTML: 所以它在HTML中以这样结束:

<img src="imageservlet/foo.png" />

Now, the second step is to create a servlet which listens on an URL pattern of /imageservlet/* , retrieves the image as an InputStream from the database by the passed-in indentifier and writes it to the OutputStream of the response along a set of correct response headers. 现在,第二步是创建一个监听/imageservlet/*的URL模式的servlet ,通过传入的标识符从数据库中将该图像作为InputStream检索,并将其写入响应的OutputStream中。正确的响应标头。 Long story short, I've posted several answers before as to how to do it, they contains kickoff code snippets: 长话短说,我之前已经发布了几个答案,如何做到这一点,它们包含启动代码片段:

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

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