Using HTML5 's file to implement Base64 and picture interchange

Source: Internet
Author: User
Tags base64 http request readfile

Just contact with the concept of an inline picture, inline picture even if the image file encoded into Base64 look at the following code is an inline problem

can reduce the HTTP request, the disadvantage is not cross-domain caching!

Copy Code code as follows:


<img src= "Data:image/jpeg;base64,/9j/4qqsrx ..." alt= "" >


And then online how to turn the picture into Base64

If you rely solely on JavaScript as a permission problem JS does not allow operation of local file files or folders for security issues

Now HTML5 came to Baidu under a lot of information in Chinese and many of the documents to the following http://www.w3.org/TR/FileAPI/

Now we use the Readasdataurl function in the HTML5 file API, which is a code that converts files into base64.

Copy Code code as follows:


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<html xmlns= "http://www.w3.org/1999/xhtml" >


<head>


<meta http-equiv= "Content-type" content= "text/html"; Charset=utf-8 "/>


<title> simple HTML5 file test for pic2base64</title>


<style>


</style>


<script>


window.onload = function () {


var input = document.getElementById ("Demo_input");


var result= document.getelementbyid ("result");


var Img_area = document.getElementById ("Img_area");


if (typeof (FileReader) = = ' undefined ') {


result.innerhtml = "Sorry, your browser does not support FileReader, please use modern browser operation!" ";


input.setattribute (' disabled ', ' disabled ');


} else {


input.addeventlistener (' Change ', readfile,false);}


}


function ReadFile () {


var file = this.files[0];


//Here we judge the next type if not the picture will be returned to remove any file can be uploaded


if (!/image/w+/.test (File.type)) {


alert ("Make sure the file is an image type");


return false;


}


var reader = new FileReader ();


reader.readasdataurl (file);


reader.onload = function (e) {


result.innerhtml = ' <img src= ' +this.result+ ' alt= '/> ';


img_area.innerhtml = ' <div class= sitetip ' > Image img Tag display: </div><img src= "' +this.result+ '" alt= "" > ';


}


}


</script>


</head>


<body>


<input type= "file" value= "SDGSDG" id= "Demo_input"/>


<textarea id= "result" rows=30 cols=300></textarea>


<p id= "Img_area" ></p>


</body>


</html>

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.