文件操作
参考:MDN-在web应用程序中使用文件
显示文件大小
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>file size</title>
</head>
<body onload="updateSize()">
<form name="uploadForm">
<div>
<input type="file" id="uploadInput" name="myFiles" onchange="updateSize();" multiple>
selected files:<span id="fileNum">0</span>
total size:<span id="fileSize">0</span>
</div>
<div><input type="submit" value="Send file"></div>
</form>
<script>
function updateSize() {
let nBytes = 0;
oFiles = document.getElementById('uploadInput').files,
nFiles = oFiles.length;
for (let nFileId = 0; nFileId < nFiles; nFileId++) {
nBytes += oFiles[nFileId].size;
}
let sOutput = nBytes + "bytes";
const aMultiples = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
for (nMultiple = 0, nApprox = nBytes / 1024; nApprox > 1; nApprox /= 1024, nMultiple++) {
sOutput = nApprox.toFixed(3) + " " + aMultiples[nMultiple] + " (" + nBytes + " bytes)";
}
document.getElementById("fileNum").innerHTML = nFiles;
document.getElementById("fileSize").innerHTML = sOutput;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>custom</title>
<style type="text/css">
#fileSelect{
background-color: aqua;
width: 130px;
height: 50px;
border-radius: 10px;
}
</style>
</head>
<body>
<input type="file" id="fileElem" multiple accept="image/*" style="display: none;" onchange="handleFiles(this.files)">
<button id="fileSelect">Select some files</button>
<script type="text/javascript">
const fileSelect = document.getElementById('fileSelect'),
fileElem = document.getElementById('fileElem');
fileSelect.addEventListener('click',(e)=>{
if(fileElem){
fileElem.click();
}
},false)
function handleFiles(files){
console.log(files);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>无绑定事件触发文件上传</title>
<style type="text/css">
.visually-hidden {
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
}
input.visually-hidden:focus+label {
outline: thin dotted;
color: red;
}
input.visually-hidden:focus-within+label {
outline: thin dotted;
color: red;
}
</style>
</head>
<body>
<input type="file" id="fileElem" multiple accept="image/*" class="visually-hidden">
<label for="fileElem">Select some files</label>
</body>
</html>
使用拖放来选择文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用拖放选择文件</title>
<style type="text/css">
#dropbox{
width: 80%;
height: 500px;
background-color: rgb(132, 161, 245);
}
</style>
</head>
<body>
<input type="file" onchange="handleFiles">
<div id="dropbox"></div>
<script type="text/javascript">
let dropbox = document.getElementById('dropbox');
dropbox.addEventListener("dragenter",dragenter,false);
dropbox.addEventListener("dragover",dragover,false);
dropbox.addEventListener("drop",drop,false);
function dragenter(e){
e.stopPropagation();
e.preventDefault();
}
function dragover(e){
e.stopPropagation();
e.preventDefault();
}
function drop(e){
e.stopPropagation();
e.preventDefault();
var dt = e.dataTransfer;
var files = dt.files;
handleFiles(files);
}
function handleFiles(files){
console.log(files);
}
</script>
</body>
</html>
显示图片缩略图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示图片缩略图</title>
<style type="text/css">
.box{
width: 100px;
height: 100px;
}
.box .img{
width: 100%;
height: auto;
}
</style>
</head>
<body>
<input type="file" multiple onchange="handleFiles(this.files)">
<div class="box"></div>
<script type="text/javascript">
function handleFiles(files){
for(let i= 0;i<files.length;i++){
var file = files[i];
console.log(file);
var imageType = /^image\//;
if(!imageType.test(file.type)){
continue;
}
var img = document.createElement('img');
var box = document.getElementsByClassName('box')[0];
img.classList.add('img');
img.file = file;
box.appendChild(img);
var reader = new FileReader();
reader.onload = (function(aImg){
return function(e){
aImg.src = e.target.result;
}
})(img)
reader.readAsDataURL(file);
}
}
</script>
</body>
</html>
使用对象URL来显示图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用对象url来显示图片</title>
</head>
<body>
<input type="file" id="fileElem" multiple accept="image/*" style="display:none;" onchange="handleFiles(this.files)">
<a href="#" id="fileSelect">Select some files</a>
<div id="fileList">
<p>No files selected!</p>
</div>
<script type="text/javascript">
window.URL = window.URL || window.webkitURL;
var fileSelect = document.getElementById("fileSelect"),
fileElem = document.getElementById("fileElem"),
fileList = document.getElementById("fileList");
fileSelect.addEventListener("click",function(e){
if(fileElem){
fileElem.click()
}
e.preventDefault();
},false)
function handleFiles(files){
if(!files.length){
fileList.innerHTML = "<p>No files selected!</p>"
}else{
fileList.innerHTML = '';
var list = document.createElement('ul');
fileList.appendChild(list);
for(let i = 0;i<files.length;i++){
var li = document.createElement("li");
list.appendChild(li);
var img = document.createElement('img');
img.src = window.URL.createObjectURL(files[i]);
img.height = 60;
img.onload = function(){
window.URL.revokeObjectURL(this.src);
}
li.appendChild(img);
var info = document.createElement('span');
info.innerHTML = files[i].name +":"+files[i].size+"bytes";
li.appendChild(info);
}
}
}
</script>
</body>
</html>
使用对象URL显示PDF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用对象URL显示PDF</title>
<style type="text/css">
#viewer{
float: right;
width: 1000px;
height: 500px;
}
</style>
</head>
<body>
<input type="file" onchange="handleFiles(this.files)">
<iframe id="viewer"></iframe>
<script type="text/javascript">
function handleFiles(files) {
console.log(files);
const obj_url = window.URL.createObjectURL(files[0]);
const iframe = document.getElementById("viewer");
iframe.setAttribute('src', obj_url);
window.URL.revokeObjectURL(obj_url);
}
</script>
</body>
</html>