forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_demo.html
More file actions
54 lines (51 loc) · 1.75 KB
/
image_demo.html
File metadata and controls
54 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>jquery image base64</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<div id="testPhone" class="weui_uploader_input_wrp" style="width:79px; height:79px;">
</div>
<hr>
<input id="testFile" type="file">
<hr>
<img id="testImg" style="max-height: 300px; height: 8em; min-width:8em;">
<script>
$("#testPhone").click(function () {
$("#testFile").click();
});
$("#testFile").change(function () {
run(this, function (data) {
$('#testImg').attr('src', data);
$('#testArea').val(data);
});
});
function run(input_file, get_data) {
/*input_file:文件按钮对象*/
/*get_data: 转换成功后执行的方法*/
if (typeof (FileReader) === 'undefined') {
alert("抱歉,你的浏览器不支持 FileReader,不能将图片转换为Base64,请使用现代浏览器操作!");
} else {
try {
/*图片转Base64 核心代码*/
var file = input_file.files[0];
//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件
if (!/image\/\w+/.test(file.type)) {
alert("请确保文件为图像类型");
return false;
}
var reader = new FileReader();
reader.onload = function () {
get_data(this.result);
}
reader.readAsDataURL(file);
} catch (e) {
alert('图片转Base64出错啦!' + e.toString())
}
}
}
</script>
</body>
</html>