JS image to Base64

Source: Internet
Author: User

JS image to Base64

Sometimes you need to insert a picture into the HTML, you can not find a suitable network after the online to store these pictures, there is no way to convert the picture into text, and then directly into the HTML, through the Base64 encoding can solve the problem.

Nonsense not much to say directly on the code, do not know what is Base64 's own Baidu.

Code
  1. <! DOCTYPE html>
  2. <meta CharSet="UTF-8">
  3. <title> JS image to Base64</title>
  4. <script src="//cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>
  5. <script>
  6. function Run (input_file,get_data) {
  7. /*input_file: File button Object */
  8. /*get_data: How to execute after successful conversion */
  9. if ( typeof(filereader) = = = ' undefined ' ) {
  10. Alert ("Sorry, your browser does not support FileReader and cannot convert the picture to Base64, please use modern browser operation!") ");
  11. } else {
  12. Try {
  13. / * image to Base64 core code * /
  14. var file = input_file. Files[0];
  15. //Here we judge the type if it is not a picture to return to remove can upload arbitrary files
  16. if (! /image\/\w+/ . Test (file. Type)) {
  17. Alert ("Make sure the file is an image type");
  18. return False ;
  19. }
  20. var reader = new filereader();
  21. Reader. onload = function (){
  22. Get_data (this. ) Result);
  23. }
  24. Reader. Readasdataurl (file);
  25. }catch (e) {
  26. Alert (' picture turns Base64 wrong! '+ e. toString ())
  27. }
  28. }
  29. }
  30. $ (function () {
  31. $ ("input"). Change (function () {
  32. Run (this, function (data) {
  33. $ (' img '). attr (' src ',data);
  34. $ (' textarea '). Val (data);
  35. });
  36. });
  37. });
  38. </script>
  39. <body>
  40. <input type="file">
  41. style="max-height:300px;" >
  42. <textarea style="Display:block; width:100%;height:30em; " ></textarea>
  43. </body>
use of Base64 pictures
BASE64 format
DATA:[][;CHARSET=][;BASE64],
The use of Base64 in CSS
. demoimg{Background-image:url ("Data:image/jpg;base64,/9j/4qmzrxhpzgaasukqaagaaaal ....");
The use of Base64 in HTML

JS image to Base64

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.