1

我今天大部分时间都在思考这个问题。我有两种触发同一事件的方法。1. 通过单击输入:文件选择器,或 2. 通过将文件拖放到“dropzone”中

事件之间的唯一区别是文件 API 使用不同的方法。似乎 eventListeners 函数调用中允许的唯一参数是(事件)。我可能错过了一些东西,但非常感谢任何帮助

但只是复制粘贴几乎相同的代码真的很烦人。有没有办法将这两个函数“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);
4

3 回答 3

2
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";
}
于 2013-02-12T16:15:41.117 回答
0

分解出通用代码:

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]);
}
于 2013-02-12T16:13:43.537 回答
0

这可能会在 @andreas 代码中更改 selectedFile = (event.dataTransfer || event.target).files[0];

selectedFile = (event.dataTransfer ? event.dataTransfer.files[0] : event.target.files[0]);
于 2013-02-12T16:21:13.537 回答