我今天大部分时间都在思考这个问题。我有两种触发同一事件的方法。1.通过单击输入:文件选择器,或2.通过将文件拖放到"dropzone"中
事件之间的唯一区别是文件API使用不同的方法。eventListeners函数调用中似乎只允许使用(event)参数。我可能错过了什么,但任何帮助都非常感谢
但仅仅复制粘贴几乎相同的代码确实很烦人。有没有办法把这两个函数"fileSelect(event)和dropSelect(event)"合为一?
代码:
function fileSelect(event){
event.stopPropagation();
event.preventDefault();
selectedFile = event.target.files[0]; //Uses ".files" from the file API in HTML5. In this case accessing info on a single file (files[0]).
//Insert file info
document.getElementById('filename').innerHTML = "filename: " + selectedFile.name;
document.getElementById('filetype').innerHTML = "filetype: " + selectedFile.type;
document.getElementById('filesize').innerHTML = "filesize: " + parseFloat(selectedFile.size/1024).toFixed(1) + " kb";
}
//parse the info from a drag'n'drop-selected image. WRITE THESE 2 FUNCTIONS INTO ONE!
function dropSelect(event){
event.stopPropagation();
event.preventDefault();
selectedFile = event.dataTransfer.files[0]; //Uses ".files" from the file API in HTML5. In this case accessing info on a single file (files[0]).
//Insert file info
document.getElementById('filename').innerHTML = "filename: " + selectedFile.name;
document.getElementById('filetype').innerHTML = "filetype: " + selectedFile.type;
document.getElementById('filesize').innerHTML = "filesize: " + parseFloat(selectedFile.size/1024).toFixed(1) + " kb";
}
//add onchange event to the file_select input:file. This will run function "fileSelect", onChange.
document.getElementById('file_select').addEventListener('change', fileSelect, false);
//start fileselect when drag'n'drop.
function handleDragOver(event){
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
}
document.getElementById('upload_box').addEventListener('dragover', handleDragOver, false);
document.getElementById('upload_box').addEventListener('drop', dropSelect, false);
function select(event){
event.stopPropagation();
event.preventDefault();
selectedFile = (event.dataTransfer || event.target).files[0];
//Insert file info
document.getElementById('filename').innerHTML = "filename: " + selectedFile.name;
document.getElementById('filetype').innerHTML = "filetype: " + selectedFile.type;
document.getElementById('filesize').innerHTML = "filesize: " + parseFloat(selectedFile.size/1024).toFixed(1) + " kb";
}
考虑通用代码:
function insertFileInfo(file) {
//Insert file info
document.getElementById('filename').innerHTML = "filename: " + file.name;
document.getElementById('filetype').innerHTML = "filetype: " + file.type;
document.getElementById('filesize').innerHTML = "filesize: " + parseFloat(file.size/1024).toFixed(1) + " kb";
}
function fileSelect(event){
event.stopPropagation();
event.preventDefault();
insertFileInfo(event.target.files[0]);
}
function dropSelect(event){
event.stopPropagation();
event.preventDefault();
insertFileInfo(event.dataTransfer.files[0]);
}
这可能有效@andreas代码的更改selectedFile=(event.dataTransfer || event.target).files[0];
至
selectedFile = (event.dataTransfer ? event.dataTransfer.files[0] : event.target.files[0]);