how to convert image to base_64 before save to storage in javascript










0














I want to convert image data to base64 string
I am already doing this technique



file 1
detail_project2.html
<form method="post" name="frm_pdf_handler" id="frm_pdf_handler" action="<?php echo base_url('index.php/laporan/pdf_handler') ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<input type="text" id="foo_element" name="grppenyerapankeuangan"/>
<button>Create PDF</button>
</form>
<script>
$(document).ready(function ()


var chart1_data = <?php echo $arrlinechart; ?>;
var chart1_options = ;
var chart1_element = 'chart_realisasi_keuangan';
var chart1_type = google.visualization.ColumnChart;
drawGoogleChart2(chart1_data, chart1_options, chart1_element, chart1_type);
);
</script>


file 2 devoops.js//from google charts



function drawGoogleChart2(chart_data, chart_options, element, chart_type) 
var data = google.visualization.arrayToDataTable(chart_data);
google.visualization.events.addListener(chart, 'ready', function ()
var image=chart.getImageURI();//convert canvas to image file from google chart
$("#foo_element").val('data:image/base64,' + image);//store base64 to input field before delivered to php controller


chart.draw(data, chart_options);
}


file 3
pdf_handler.php (codeigniter controller)



<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pdf_handler extends CI_Controller
function index()
echo "<img src='".$this->input->post("foo_element")."'"."/>";




base64 string is successfully generated but cannot apears image as my espectation. someone can help me please?










share|improve this question























  • $("#foo_element") is an image element? if yeas then you need to change src of element. like $("#foo_element").attr('src', <your base64 variable here>)
    – Hardik
    Nov 12 at 5:19











  • no it's not. $("#foo_element") is variabel who I use to store base64 value before I delivere it to codeigniter controller
    – Alimin
    Nov 12 at 6:54










  • OK, Then find img element by selector (if not then add to html page) and apply src to it as your base64 string is successfully generated and you want display it in image.
    – Hardik
    Nov 12 at 7:07










  • please look at my answer on file 3 pdf_handler.php I am already doing that in php file like this echo "<img src='".$this->input->post("foo_element")."'"."/>" , my problem is when I convert google chart canvas to base64 the result failed to render to be an image again
    – Alimin
    Nov 12 at 7:15















0














I want to convert image data to base64 string
I am already doing this technique



file 1
detail_project2.html
<form method="post" name="frm_pdf_handler" id="frm_pdf_handler" action="<?php echo base_url('index.php/laporan/pdf_handler') ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<input type="text" id="foo_element" name="grppenyerapankeuangan"/>
<button>Create PDF</button>
</form>
<script>
$(document).ready(function ()


var chart1_data = <?php echo $arrlinechart; ?>;
var chart1_options = ;
var chart1_element = 'chart_realisasi_keuangan';
var chart1_type = google.visualization.ColumnChart;
drawGoogleChart2(chart1_data, chart1_options, chart1_element, chart1_type);
);
</script>


file 2 devoops.js//from google charts



function drawGoogleChart2(chart_data, chart_options, element, chart_type) 
var data = google.visualization.arrayToDataTable(chart_data);
google.visualization.events.addListener(chart, 'ready', function ()
var image=chart.getImageURI();//convert canvas to image file from google chart
$("#foo_element").val('data:image/base64,' + image);//store base64 to input field before delivered to php controller


chart.draw(data, chart_options);
}


file 3
pdf_handler.php (codeigniter controller)



<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pdf_handler extends CI_Controller
function index()
echo "<img src='".$this->input->post("foo_element")."'"."/>";




base64 string is successfully generated but cannot apears image as my espectation. someone can help me please?










share|improve this question























  • $("#foo_element") is an image element? if yeas then you need to change src of element. like $("#foo_element").attr('src', <your base64 variable here>)
    – Hardik
    Nov 12 at 5:19











  • no it's not. $("#foo_element") is variabel who I use to store base64 value before I delivere it to codeigniter controller
    – Alimin
    Nov 12 at 6:54










  • OK, Then find img element by selector (if not then add to html page) and apply src to it as your base64 string is successfully generated and you want display it in image.
    – Hardik
    Nov 12 at 7:07










  • please look at my answer on file 3 pdf_handler.php I am already doing that in php file like this echo "<img src='".$this->input->post("foo_element")."'"."/>" , my problem is when I convert google chart canvas to base64 the result failed to render to be an image again
    – Alimin
    Nov 12 at 7:15













0












0








0


1





I want to convert image data to base64 string
I am already doing this technique



file 1
detail_project2.html
<form method="post" name="frm_pdf_handler" id="frm_pdf_handler" action="<?php echo base_url('index.php/laporan/pdf_handler') ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<input type="text" id="foo_element" name="grppenyerapankeuangan"/>
<button>Create PDF</button>
</form>
<script>
$(document).ready(function ()


var chart1_data = <?php echo $arrlinechart; ?>;
var chart1_options = ;
var chart1_element = 'chart_realisasi_keuangan';
var chart1_type = google.visualization.ColumnChart;
drawGoogleChart2(chart1_data, chart1_options, chart1_element, chart1_type);
);
</script>


file 2 devoops.js//from google charts



function drawGoogleChart2(chart_data, chart_options, element, chart_type) 
var data = google.visualization.arrayToDataTable(chart_data);
google.visualization.events.addListener(chart, 'ready', function ()
var image=chart.getImageURI();//convert canvas to image file from google chart
$("#foo_element").val('data:image/base64,' + image);//store base64 to input field before delivered to php controller


chart.draw(data, chart_options);
}


file 3
pdf_handler.php (codeigniter controller)



<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pdf_handler extends CI_Controller
function index()
echo "<img src='".$this->input->post("foo_element")."'"."/>";




base64 string is successfully generated but cannot apears image as my espectation. someone can help me please?










share|improve this question















I want to convert image data to base64 string
I am already doing this technique



file 1
detail_project2.html
<form method="post" name="frm_pdf_handler" id="frm_pdf_handler" action="<?php echo base_url('index.php/laporan/pdf_handler') ?>">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name();?>" value="<?php echo $this->security->get_csrf_hash();?>">
<input type="text" id="foo_element" name="grppenyerapankeuangan"/>
<button>Create PDF</button>
</form>
<script>
$(document).ready(function ()


var chart1_data = <?php echo $arrlinechart; ?>;
var chart1_options = ;
var chart1_element = 'chart_realisasi_keuangan';
var chart1_type = google.visualization.ColumnChart;
drawGoogleChart2(chart1_data, chart1_options, chart1_element, chart1_type);
);
</script>


file 2 devoops.js//from google charts



function drawGoogleChart2(chart_data, chart_options, element, chart_type) 
var data = google.visualization.arrayToDataTable(chart_data);
google.visualization.events.addListener(chart, 'ready', function ()
var image=chart.getImageURI();//convert canvas to image file from google chart
$("#foo_element").val('data:image/base64,' + image);//store base64 to input field before delivered to php controller


chart.draw(data, chart_options);
}


file 3
pdf_handler.php (codeigniter controller)



<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pdf_handler extends CI_Controller
function index()
echo "<img src='".$this->input->post("foo_element")."'"."/>";




base64 string is successfully generated but cannot apears image as my espectation. someone can help me please?







javascript base64






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:44

























asked Nov 12 at 3:47









Alimin

3181314




3181314











  • $("#foo_element") is an image element? if yeas then you need to change src of element. like $("#foo_element").attr('src', <your base64 variable here>)
    – Hardik
    Nov 12 at 5:19











  • no it's not. $("#foo_element") is variabel who I use to store base64 value before I delivere it to codeigniter controller
    – Alimin
    Nov 12 at 6:54










  • OK, Then find img element by selector (if not then add to html page) and apply src to it as your base64 string is successfully generated and you want display it in image.
    – Hardik
    Nov 12 at 7:07










  • please look at my answer on file 3 pdf_handler.php I am already doing that in php file like this echo "<img src='".$this->input->post("foo_element")."'"."/>" , my problem is when I convert google chart canvas to base64 the result failed to render to be an image again
    – Alimin
    Nov 12 at 7:15
















  • $("#foo_element") is an image element? if yeas then you need to change src of element. like $("#foo_element").attr('src', <your base64 variable here>)
    – Hardik
    Nov 12 at 5:19











  • no it's not. $("#foo_element") is variabel who I use to store base64 value before I delivere it to codeigniter controller
    – Alimin
    Nov 12 at 6:54










  • OK, Then find img element by selector (if not then add to html page) and apply src to it as your base64 string is successfully generated and you want display it in image.
    – Hardik
    Nov 12 at 7:07










  • please look at my answer on file 3 pdf_handler.php I am already doing that in php file like this echo "<img src='".$this->input->post("foo_element")."'"."/>" , my problem is when I convert google chart canvas to base64 the result failed to render to be an image again
    – Alimin
    Nov 12 at 7:15















$("#foo_element") is an image element? if yeas then you need to change src of element. like $("#foo_element").attr('src', <your base64 variable here>)
– Hardik
Nov 12 at 5:19





$("#foo_element") is an image element? if yeas then you need to change src of element. like $("#foo_element").attr('src', <your base64 variable here>)
– Hardik
Nov 12 at 5:19













no it's not. $("#foo_element") is variabel who I use to store base64 value before I delivere it to codeigniter controller
– Alimin
Nov 12 at 6:54




no it's not. $("#foo_element") is variabel who I use to store base64 value before I delivere it to codeigniter controller
– Alimin
Nov 12 at 6:54












OK, Then find img element by selector (if not then add to html page) and apply src to it as your base64 string is successfully generated and you want display it in image.
– Hardik
Nov 12 at 7:07




OK, Then find img element by selector (if not then add to html page) and apply src to it as your base64 string is successfully generated and you want display it in image.
– Hardik
Nov 12 at 7:07












please look at my answer on file 3 pdf_handler.php I am already doing that in php file like this echo "<img src='".$this->input->post("foo_element")."'"."/>" , my problem is when I convert google chart canvas to base64 the result failed to render to be an image again
– Alimin
Nov 12 at 7:15




please look at my answer on file 3 pdf_handler.php I am already doing that in php file like this echo "<img src='".$this->input->post("foo_element")."'"."/>" , my problem is when I convert google chart canvas to base64 the result failed to render to be an image again
– Alimin
Nov 12 at 7:15












2 Answers
2






active

oldest

votes


















1














Please try to set it like this:



$('#foo_element').html('<img src="data:image/png;base64,' + image + '" />');


Update



Can you please attach the html from what this line outputs (pdf_handler.php),



echo "<img src='".$this->input->post("grppenyerapankeuangan")."'"."img/>"; 


it looks like it should be like this



echo "<img src='".$this->input->post("grppenyerapankeuangan")."' />"; 


Also in my example it specifies image type as png, not sure if image type is mandatory..






share|improve this answer






















  • $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
    – Alimin
    Nov 12 at 4:10










  • Can you please provide your complete html and php code..
    – bestinamir
    Nov 12 at 4:21










  • okay please wait..
    – Alimin
    Nov 12 at 4:28










  • pastebin.com/1duBPuxZ here my complete code
    – Alimin
    Nov 12 at 4:48










  • @Alimin, please see my updated answer above..
    – bestinamir
    Nov 12 at 5:41


















1














[solved]


this is actualy happen.



when I am doing this



var image=chart.getImageURI();


variabel image is not image file but base64 encoding format
so my mistake is
when I am trying to convert base64 again
like this



$("#foo_element").val('data:image/base64,' + image);//my fatal mistake


so, this is what I necesary to do



$("#foo_element").val(image);//my revision


and finaly everything work fine.
thank you All :D






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255711%2fhow-to-convert-image-to-base-64-before-save-to-storage-in-javascript%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Please try to set it like this:



    $('#foo_element').html('<img src="data:image/png;base64,' + image + '" />');


    Update



    Can you please attach the html from what this line outputs (pdf_handler.php),



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."'"."img/>"; 


    it looks like it should be like this



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."' />"; 


    Also in my example it specifies image type as png, not sure if image type is mandatory..






    share|improve this answer






















    • $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
      – Alimin
      Nov 12 at 4:10










    • Can you please provide your complete html and php code..
      – bestinamir
      Nov 12 at 4:21










    • okay please wait..
      – Alimin
      Nov 12 at 4:28










    • pastebin.com/1duBPuxZ here my complete code
      – Alimin
      Nov 12 at 4:48










    • @Alimin, please see my updated answer above..
      – bestinamir
      Nov 12 at 5:41















    1














    Please try to set it like this:



    $('#foo_element').html('<img src="data:image/png;base64,' + image + '" />');


    Update



    Can you please attach the html from what this line outputs (pdf_handler.php),



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."'"."img/>"; 


    it looks like it should be like this



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."' />"; 


    Also in my example it specifies image type as png, not sure if image type is mandatory..






    share|improve this answer






















    • $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
      – Alimin
      Nov 12 at 4:10










    • Can you please provide your complete html and php code..
      – bestinamir
      Nov 12 at 4:21










    • okay please wait..
      – Alimin
      Nov 12 at 4:28










    • pastebin.com/1duBPuxZ here my complete code
      – Alimin
      Nov 12 at 4:48










    • @Alimin, please see my updated answer above..
      – bestinamir
      Nov 12 at 5:41













    1












    1








    1






    Please try to set it like this:



    $('#foo_element').html('<img src="data:image/png;base64,' + image + '" />');


    Update



    Can you please attach the html from what this line outputs (pdf_handler.php),



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."'"."img/>"; 


    it looks like it should be like this



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."' />"; 


    Also in my example it specifies image type as png, not sure if image type is mandatory..






    share|improve this answer














    Please try to set it like this:



    $('#foo_element').html('<img src="data:image/png;base64,' + image + '" />');


    Update



    Can you please attach the html from what this line outputs (pdf_handler.php),



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."'"."img/>"; 


    it looks like it should be like this



    echo "<img src='".$this->input->post("grppenyerapankeuangan")."' />"; 


    Also in my example it specifies image type as png, not sure if image type is mandatory..







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 5:09

























    answered Nov 12 at 3:58









    bestinamir

    596




    596











    • $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
      – Alimin
      Nov 12 at 4:10










    • Can you please provide your complete html and php code..
      – bestinamir
      Nov 12 at 4:21










    • okay please wait..
      – Alimin
      Nov 12 at 4:28










    • pastebin.com/1duBPuxZ here my complete code
      – Alimin
      Nov 12 at 4:48










    • @Alimin, please see my updated answer above..
      – bestinamir
      Nov 12 at 5:41
















    • $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
      – Alimin
      Nov 12 at 4:10










    • Can you please provide your complete html and php code..
      – bestinamir
      Nov 12 at 4:21










    • okay please wait..
      – Alimin
      Nov 12 at 4:28










    • pastebin.com/1duBPuxZ here my complete code
      – Alimin
      Nov 12 at 4:48










    • @Alimin, please see my updated answer above..
      – bestinamir
      Nov 12 at 5:41















    $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
    – Alimin
    Nov 12 at 4:10




    $("#foo_element") is an input element who I use to store base64 before I deliver it to php controller.
    – Alimin
    Nov 12 at 4:10












    Can you please provide your complete html and php code..
    – bestinamir
    Nov 12 at 4:21




    Can you please provide your complete html and php code..
    – bestinamir
    Nov 12 at 4:21












    okay please wait..
    – Alimin
    Nov 12 at 4:28




    okay please wait..
    – Alimin
    Nov 12 at 4:28












    pastebin.com/1duBPuxZ here my complete code
    – Alimin
    Nov 12 at 4:48




    pastebin.com/1duBPuxZ here my complete code
    – Alimin
    Nov 12 at 4:48












    @Alimin, please see my updated answer above..
    – bestinamir
    Nov 12 at 5:41




    @Alimin, please see my updated answer above..
    – bestinamir
    Nov 12 at 5:41













    1














    [solved]


    this is actualy happen.



    when I am doing this



    var image=chart.getImageURI();


    variabel image is not image file but base64 encoding format
    so my mistake is
    when I am trying to convert base64 again
    like this



    $("#foo_element").val('data:image/base64,' + image);//my fatal mistake


    so, this is what I necesary to do



    $("#foo_element").val(image);//my revision


    and finaly everything work fine.
    thank you All :D






    share|improve this answer

























      1














      [solved]


      this is actualy happen.



      when I am doing this



      var image=chart.getImageURI();


      variabel image is not image file but base64 encoding format
      so my mistake is
      when I am trying to convert base64 again
      like this



      $("#foo_element").val('data:image/base64,' + image);//my fatal mistake


      so, this is what I necesary to do



      $("#foo_element").val(image);//my revision


      and finaly everything work fine.
      thank you All :D






      share|improve this answer























        1












        1








        1






        [solved]


        this is actualy happen.



        when I am doing this



        var image=chart.getImageURI();


        variabel image is not image file but base64 encoding format
        so my mistake is
        when I am trying to convert base64 again
        like this



        $("#foo_element").val('data:image/base64,' + image);//my fatal mistake


        so, this is what I necesary to do



        $("#foo_element").val(image);//my revision


        and finaly everything work fine.
        thank you All :D






        share|improve this answer












        [solved]


        this is actualy happen.



        when I am doing this



        var image=chart.getImageURI();


        variabel image is not image file but base64 encoding format
        so my mistake is
        when I am trying to convert base64 again
        like this



        $("#foo_element").val('data:image/base64,' + image);//my fatal mistake


        so, this is what I necesary to do



        $("#foo_element").val(image);//my revision


        and finaly everything work fine.
        thank you All :D







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 7:29









        Alimin

        3181314




        3181314



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255711%2fhow-to-convert-image-to-base-64-before-save-to-storage-in-javascript%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3