0

I need to send a image/media through json, for that conversion needs to be done into text format. How can I achieve that through jQuery/ Javascript?

2 Answers 2

1

You can find your answer in this post get image data in javascript

function getBase64Image(img) {
    // Create an empty canvas element
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    // Copy the image contents to the canvas
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    // Get the data-URL formatted image
    // Firefox supports PNG and JPEG. You could check img.src to guess the
    // original format, but be aware the using "image/jpg" will re-encode the image.
    var dataURL = canvas.toDataURL("image/png");

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

You need to pass the img tag to this function. For further details see Convert an image into binary data in javascript

Sign up to request clarification or add additional context in comments.

Comments

0

A few times already I read that jQuery doesn't provide functionality to download binary data and have it pass on to JavaScript as a string. Then I ran into this question. That got me thinking and I wrote a wrapper around $.ajax() looking like this (yes, it's simplified to show the main bits):

ajaxWrapper = function(url, dataType, callback, headers) {
    return $.ajax({
        url: url,
        dataType: dataType == "binary" ? "text" : dataType,
        mimeType: dataType == "binary" ? "text/plain; charset=x-user-defined" : undefined,
        headers: headers || {}
    }).done(function(data, status, jqXHR) {
        callback(data, status, jqXHR);
    });
}

And then in case you are dealing with Unicode the callback contains this line:

data = btoa(unescape(encodeURIComponent(data)));

or otherwise simply

data = btoa(data);

In other words if you read through the documentation of $.ajax() you simply add a dataType "binary".

Note that I use jQuery 1.7.1, but I don't see why it shouldn't work in later versions, too.

4 Comments

decodeURIComponent(encodeURIComponent(…)) obviously is senseless.
@Bergi Well, actually not. If you google for base64-encoding and browser issues you will run into answers suggesting to use this.
Can you point me to it? However I haven't seen a browser where decoding is not the inverse function of encoding, even if programmed non-standard.
@Bergi First, I forgot to say that it's related to Unicode-strings. Second, this is weird. If I google for it I get developer.mozilla.org/en-US/docs/DOM/window.btoa as the third hit. However, the content of the page doesn't have that line in there any more. I will change my answer to use unescape(). Thanks for the hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.