views:

234

answers:

3

I'm trying to read image from database with Ajax, but I could not read the xmlhttp.responseText to the img src. The image is saved as binary data in database and also retrieved as binary data. I'm using Ajax in JSP, because I want to give the user the ability to upload images and I will view the last uploaded image, on mouse over action the Ajax will be activated and get the image back, the problem is in reading the img from the response.

This is the Ajax function:

function ajaxFunction(path) {
    if (xmlhttp) { 
        var s = path;
        xmlhttp.open("GET", s, true); 
        xmlhttp.onreadystatechange = handleServerResponse;
        xmlhttp.send(null);
    }
}

function handleServerResponse() {
    if (xmlhttp.readyState == 4) {
        var Image = document.getElementById(Image_Element_Name);
        document.getElementById(Image_Element_Name).src = "data:" + xmlhttp.responseText;
    }
}

I also got exception in the server:

10415315 [TP-Processor1] WARN core.MsgContext  - Error sending end packet
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at org.apache.jk.common.ChannelSocket.send(ChannelSocket.java:537)
    at org.apache.jk.common.JkInputStream.endMessage(JkInputStream.java:127)
    at org.apache.jk.core.MsgContext.action(MsgContext.java:302)
    at org.apache.coyote.Response.action(Response.java:183)
    at org.apache.coyote.Response.finish(Response.java:305)
    at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:281)
    at org.apache.catalina.connector.Response.finishResponse(Response.java:478)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:154)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:200)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:773)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:895)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
    at java.lang.Thread.run(Thread.java:619)
10415316 [TP-Processor1] WARN common.ChannelSocket  - processCallbacks status 2
+1  A: 

Why are you storing the images in the DB? Save them as files and save the URL in the DB.

That way you can use AJAX to call a PHP that will read the DB, pull out the URL for the image and return it to you, so you can do something like:

img = document.getElementById("myimage");
img.src = URL; // where URL will contain the URL you got from the AJAX call 
nico
I don't want to use files , and I'm restrict with using AJAX and JSP files
ama
May I ask why? Interesting discussion on saving in the filesystem vs the DB http://www.webmasterworld.com/forum88/9091.htm
nico
As suggested before I think it's best to just set the src to the URL that fetches you the image. This also avoids having a lot of code that just gets an image from a URL!
filip-fku
because i want to be able to get the old images for each user any time i want
ama
Storing them as files won't prevent you from doing that.
David Dorward
yes i know , but as i said i need to go with this way , there is any suggestion how to get the src from the response ??
ama
the src is the URL you make the xmlhttprequest to..
filip-fku
can u explain more plz..
ama
A: 

try this link

mabuzer
A: 

That's not the way how it works. As answered before, you basically just need to change the src attribute of the <img> element to a different URL. The webbrowser will then reload the image itself automagically.

In case of JSP/Servlet, the URL should just point to a Servlet class which obtains an InputStream of the image from the database and writes it to the OutputStream of the response the usual Java IO way along with a set of response headers.

Here is how your Javascript instead should look like:

function changeImage(newSrc) {
    document.getElementById(Image_Element_Name).src = newSrc;
}

In this answer you can find a complete example how the Servlet should look like.


The SocketException: broken pipe is just a sign that the client side didn't process the response at all and aborted it. If you get rid of that Ajax stuff and do it as explained above, then it should work flawlessly.

BalusC
yes I see ,that is what I did for obtain the image from DB the problem was in sending the request for the jsp file ,when I add the time as parameter to the request ,the browser found new request each time ,so there will be new src for the image and the browser will load the image.
ama
So, what's the problem?
BalusC
there is no problem now , i solve it ,thnx
ama
Ah OK. You're welcome. I however see that you're new here. You are supposed to vote the useful answers (click the up arrow) and accept the most helpful answer (click the checkmark). Also see this link to learn how Stackoverflow works: http://stackoverflow.com/faq :)
BalusC