简体   繁体   中英

convert to base64 and send image using ajax in Javascript

I need to send selected image to the server, here is already I have tried

here is HTML part

                    <div class="row">
                        <input type="file" name="filUpload" id="filUpload" onchange="showimagepreview(this)">
                        <br />
                        <img id="imgprvw" alt="uploaded image preview" class="img-thumbnail" />
                    </div>

I need to get encoded image into javascript variable,But I have no idea

for uploading image on to server you need to send image data via AJAX to server side.

for reference you can see this link :

http://www.sanwebe.com/2012/05/ajax-image-upload-and-resize-with-jquery-and-php

http://phppot.com/php/php-ajax-image-upload/

you can try using jquery's base64 plugin. Jquery.Base64 plugin

and do it like this:

function showimagepreview(c) {
    var file = c;
    if(file.files.length)
    {
        var reader = new FileReader();
        reader.onload = function(e)
        {
            var b = $.base64.encode(e.target.result);

            $("#imgprvw").attr("src", "data:image/png;base64," + b);
        };
        reader.readAsBinaryString(file.files[0]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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