Skip to content

Instantly share code, notes, and snippets.

@MrFant
Last active June 14, 2018 13:35
Show Gist options
  • Save MrFant/95b5529c11414809003ae589a75e2602 to your computer and use it in GitHub Desktop.
Save MrFant/95b5529c11414809003ae589a75e2602 to your computer and use it in GitHub Desktop.
html5 file api demo
<html>
<head>
<meta charset="UTF-8">
<title>File API</title>
</head>
<body>
<article>
<div id="holder"></div>
<p id="status">File API & FileReader API not supported</p>
<p>Drag an image from your desktop on to the drop zone above to see the browser read the contents of the file and use it as the background - without uploading the file to any servers.</p>
</article>
<style>
#holder { border: 10px dashed #ccc; width: 300px; height: 300px; margin: 20px auto;}
#holder.hover { border: 10px dashed #333; }
</style>
<script>
var holder = document.getElementById('holder'),
state = document.getElementById('status');
if (typeof window.FileReader === 'undefined') {
state.className = 'fail';
} else {
state.className = 'success';
state.innerHTML = 'File API & FileReader available';
}
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
this.className = '';
e.preventDefault();
var file = e.dataTransfer.files[0],
reader = new FileReader();
reader.onload = function (event) {
console.log(event.target);
holder.style.background = 'url(' + event.target.result + ') no-repeat center';
};
console.log(file);
reader.readAsDataURL(file);
return false;
};
</script>
</body>
</html>
<!--
/**
* html5 file api实例
* input type=file 时,会显示一个choose Files的按钮,按钮旁是一段描述,这个显示效果的原理暂不明了。
* 当input标签设置了multiple属性时,可以上传多个文件,所以在获取file引用时需要加上[0].
* FileReader() api 待了解。。。
*
*
*/
-->
<!DOCTYPE html>
<html>
<head>
<title>untitled</title>
</head>
<body>
<form method="post" action="http://localhost/test" enctype="multipart/form-data">
<p>图片预览:</p>
<p>
<div id="test-image-preview"
style="border: 1px solid #ccc; width: 100%; height: 200px; background-size: contain; background-repeat: no-repeat; background-position: center center;"></div>
</p>
<p>
<input type="file" multiple id="test-image-file" name="test">
</p>
<p id="test-file-info"></p>
</form>
<script>
var
fileInput = document.getElementById('test-image-file'),
info = document.getElementById('test-file-info'),
preview = document.getElementById('test-image-preview');
// 监听change事件:
fileInput.addEventListener('change', function () {
// 清除背景图片:
preview.style.backgroundImage = '';
// 检查文件是否选择:
if (!fileInput.value) {
info.innerHTML = '没有选择文件';
return;
}
// 获取File引用:
var file = fileInput.files[0];
// 获取File信息:
info.innerHTML =
'文件: ' + file.name + '<br>' +
'大小: ' + file.size + '<br>' +
'修改: ' + file.lastModifiedDate;
if (file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
alert('不是有效的图片文件!');
return;
}
// 读取文件:
var reader = new FileReader();
reader.onload = function (e) {
var
data = e.target.result; // 'data:image/jpeg;base64,/9j/4AAQSk...(base64编码)...'
preview.style.backgroundImage = 'url(' + data + ')';
};
// 以DataURL的形式读取文件:
reader.readAsDataURL(file);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment