html5图片预览

html5实现了对已经编码的base64的图片进行展示,运用这一点对图片实现预览,

如下显示的是chrome的图标:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAuAC4DAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD+gjWNe0PwnoGq+JPEmr2Wi6Ho1ncahqmq6hOlvZ2Nlb73lmmldgEwo2hRl5HCxRo0xRW/tvNczy/JsBjs1zXF0MDl+AoVMTjMbiaipYfDYejGc51atSTSUUltvJ3jHmklF/4rcH8IcTcecTZJwbwbkWZcScU8SZ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sj9W42+nH49ca4Kvl2FznL+D8HiIypVocJ4GeX4mUJuelPNMVi8wzfCuMWoqeEzChNq/tJTtr9N6F4e0bwzp8Gm6HYW+nWNuqxxwW0axj5QwDHABZjwSzEsT67ef7Gw1HDYOhSwuEw9LD4fD0qdGhQoQhSpUqNOMoUqVKnCKhTp04csYU4JRjFKMUoo/kzEYjF4zEVsVjcTWxeJrVJ1atevUnUq1alScpzqVak5SlOcpNynOTcpyk5SlzK730jbDAN02cnng78dfcEn8OSSxNPW97dOiXWXS1uuna71a0MopOPNey0SSXnO2nN5fi+7P/Z" alt=""/>

下面是一个简单的前端html+js,显示图片并预览

<!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>简单的html5 File测试 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 = "抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!"; 
input.setAttribute( 'disabled','disabled' ); 
} else { 
input.addEventListener( 'change',readFile,false );} 
} 
function readFile(){ 
var file = this.files[0]; 
//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件 
if(!/image\/\w+/.test(file.type)){ 
alert("请确保文件为图像类型"); 
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">图片img标签展示:</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> 


相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页