-
Notifications
You must be signed in to change notification settings - Fork 83
/
fileReader.html
65 lines (61 loc) · 1.65 KB
/
fileReader.html
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
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>FileReader Sample</title>
</head>
<script language="javascript">
var result = document.getElementById("result");
var file=document.getElementById("file");
if(typeof FileReader == 'undefined')
{
result.innerHTML = "<p>Sorry Your Browser doesn't support FileReader</p>";
file.setAtrribute('disabled','disabled');
}
function readAsDataURL()
{
var file=document.getElementById("file").files[0];
if(!/image\/\w+/.test(file.type))
{
alert("make sure your file type is image");
return false;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e)
{
var result = document.getElementById("result");
result.innerHTML = '<img src="' + this.result + '" alt="" />';
}
}
function readAsBinnaryString()
{
var file=document.getElementById("file").files[0];
var reader = new FileReader();
reader.readAsBinnaryString(file);
reader.onload = function(f)
{
var result = document.getElementById("result");
result.innerHTML = this.result;
}
}
function readAsText()
{
var file = document.getElementById("file").files[0];
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(f)
{
var result = document.getElementById("result");
result.innerHTML = this.result;
}
}
</script>
<p>
<label>Please Choose a file</label>
<input type="file" id="file"/>
<input type="button" value="Read Image" onclick="readAsDataURL()" />
<input type="button" value="Read Binary" onclick="readAsBinaryString()" />
<input type="button" value="Read Text" onclick="readAsText()" />
</p>
<div name="result" id="result">
</div>