title: post base64 encode image to server using javascript
body: <p>upload image to server ,image is from ,then i want to post image to server through ajax, server side using python flask framework, it needs base64 encoding format  ,the question is how can i use javascript to convert image to base64 format.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> $('.img-upload-btn').click(function(event) {
    $("#img-upload").click();
   });
  
   $('#img-upload').on('change', function(event) {
    event.preventDefault();
    var img = $("#img-upload")[0].files[0];
    console.log(toDataUrl(img.name));
  
    var img_data = {
     "spec_id": 212,
     "file": img
    };
    console.log(img);
    $.ajax({
     url: 'http://10.0.0.75:5000/api/check_specification',
     type: 'POST',
     dataType: 'json',
     contentType: "application/json; charset=utf-8",
     success: function(data) {
      alert(data);
     },
  
     failure:function(errorMsg) {
      alert(errorMsg);
     }
    });
    
   });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;button class="btn-search full-width img-upload-btn"&gt;upload-img&lt;/button&gt;
  &lt;input type="file" id="img-upload" &gt;</code></pre>
  </div>
  </div>
  </p>