Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4076692
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:27:27+00:00 2026-05-20T17:27:27+00:00

This should be simple enough, but after wrestling with it for hours, I still

  • 0

This should be simple enough, but after wrestling with it for hours, I still can’t get it to work. So far, all my attempts have resulted in the image becoming ‘corrupted or truncated’, according to firefox.

Retrieve the image from the server with a jquery-ajax call:

 $.ajax({
                async: false,
                url: db[key]["DocumentLink"],
                success: function (result2) {

Base64 encode the image, and store it in localStore:
In this example I’m using the jquery base64-encoding plugin, but I’ve tried several.

                        var dbKey = "Doc " + db[key]["ID"] + " " + db[key]["Title"];
                        console.log("storing: " + db[key]["DocumentLink"] + " in " + dbKey + "\n");
                        localStorage.removeItem(dbKey);
                        var base64Image = $.base64Encode(result2);
                        console.log(base64Image.length);
                        localStorage.setItem(dbKey, base64Image);
                       console.log("is stored: " + db[key]["DocumentLink"] + " in " + dbKey + "\n");
                }
})

Display the image with a data url:

function openImageFromDB(dbKey) {
    console.log("Trying to display image with key " + dbKey);
    var base64Img = localStorage.getItem(dbKey);
    document.getElementById("documentHolder").src='data:image/jpeg;base64,' + base64Img;
}

The corresponding img:

 <img id="documentHolder" alt="Image view placeholder" src="" />

However, on every try, firefox displays:

Image corrupt or truncated: data:image/jpeg;base64,77+977+977+977+9a<... much longer string>

The Url: points to a valid jpeg image, and the base64Image.length and the error message show that the var / localStorage actually contain what seems to be base64 encoded data.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:27:28+00:00Added an answer on May 20, 2026 at 5:27 pm
    • DEMO: http://so.lucafilosofi.com/retrieve-an-image-from-the-server-store-it-in-localstorage-and-display-it

    Javascript (AJAX call)

    function LoadImg(filename) {
        var xmlhttp;
        if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {     
                document.getElementById("documentHolder").src = "data:image/png;base64," + xmlhttp.responseText;
            }
        };    
        xmlhttp.open("GET", 'load.php?LoadImg='+filename );
        xmlhttp.send(null);
    }
    

    PHP ( load.php )

    <?php
     if (isset($_GET['LoadImg'])) {
      header("Content-Type: image/png");
      $file = file_get_contents($_GET['LoadImg']);
      echo base64_encode($file);
    }
    ?>
    

    Read this may help you:

    • Base 64 encode vs loading an image file
    • How to encode image data within an HTML file?
    • How can you encode a string to Base64 in JavaScript?
    • Get image data in JavaScript?

    PS: maybe your Base64 is wrong?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi, I know this should be really simple but I am just too new
This should be a simple one, and yet I need help to solve the
I wrote a simple daemon. This daemon should respond when I run any program.
I guess what I am trying to do should be simple, but don’t know
Is there a better way of writing this code? It just doesn't sit right
I don't even know the proper terminology for this lisp syntax, so I don't
I want to do something with ehcache in Java that I think should be
I'm writing an application draws in another application's window (this is under OS X
I am supposed to provide my users a really simple way of capturing video
I wrote two simple programs server and a client using sockets in C++ (Linux).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.