SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool Problem echoing back image to xmlhttp request

    Hi,

    I have an xmlhttp request that calls on a php page. The php page just reads an image file from the file system and echos it back. But in this case, the image will not be visible.

    Please note that I can get text and html tagged images (<img>) in this way using xmlhttp requests. I have made a workaround by calling an html page with an <img src="... the image php file">, but it is not a real solution to this problem.

    It might have something to do with setting request headers, but had no luck with them, e.g. page_request.setRequestHeader("Content-Type: image/jpeg") didn't do any good.

    I would appreciate any help. Let me know if you need further information.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Clearly state the following:

    1) What your code does now.

    2) What you would like for your code to do.

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) What your code does now.

    On the calling page, the layer that is supposed to show the image stays blank:

    page_request = new XMLHttpRequest();
    page_request.onreadystatechange=function(){
    loadpage(page_request, containerid);
    }
    page_request.open('GET', 'image.php', true);
    page_request.send(null);

    function loadpage(page_request, containerid){
    if (page_request.readyState == 4 && page_request.status==200)
    document.getElementById(containerid).innerHTML=page_request.responseText;
    }

    2) What you would like for your code to do.

    I want to show the image that php reads from file 'image.php':

    <?php
    $file_size = filesize($file1);
    $fp = fopen($file1, "r");
    if($file_size) $file_content = fread($fp, $file_size);

    fclose($fp); //Close the file

    header("Content-Type: image/jpeg");
    header("Content-Disposition: inline");
    echo $file_content;
    ?>


    The xmlhttp request calls upon the above code to provide it with the image, but the image doesn't show.

  4. #4
    SitePoint Addict NikoB's Avatar
    Join Date
    Nov 2005
    Location
    Austria
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am pretty sure it's not possible to change/set an image with innerHTML. Why don't you just change the src of the image element?
    document.getElementById(containerid).src = "image.php"

    this should work (and thats the way those graphical counters work....)
    Corinis OpenSource Community & Content Management
    http://www.corinis.org

  5. #5
    SitePoint Member
    Join Date
    Dec 2004
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks NikoB for your solution! It works fine. Since in my application the image changes, I was trying to get rid of caching the image altogether using xmlhttp requests and streaming back the image, but it seems not to be a solution.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •