javascript HTML 5 文件上传事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8560694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:48:31 来源:igfitidea点击:
HTML 5 File upload event
提问by Maximilian Lindsey
I am trying to upload a file using the HTML File Api but I really seem not to get how to do it. What event do I have to give to the getImg() function?
我正在尝试使用 HTML File Api 上传文件,但我似乎真的不知道该怎么做。我必须为 getImg() 函数提供什么事件?
HTML
HTML
<input id='img' type='file' onchange='getImg(event)'/>
JS
JS
function getImg(evt){
var files = evt.dataTransfer.files;
var file = files[0];
console.log(file.name)
回答by Maurice
The dataTransfer object is for drag&drop operations. Use the target instead.
dataTransfer 对象用于拖放操作。改用目标。
<!DOCTYPE html>
<html>
<body>
<input id='img' type='file' onchange='getImg(event)'/>
<script>
function getImg(evt){
var files = evt.target.files;
var file = files[0];
console.log(file.name);
}
</script>
</body>
</html>