如何检测React中<input type =“file”/>的空选择?

时间:2017-06-18 12:23:26

标签: file reactjs input components

我有一个使用<input type="file" value="" onChange={(e) => callback(e.target.files}/>来获取用户所选文件的组件。但是,如果用户取消文件选择弹出窗口,则永远不会调用回调,因为没有更改。这是无稽之谈,因为预期的操作是使用null或空数组调用回调。

但是如何实现呢?我没有找到任何相关的事件来听。

1 个答案:

答案 0 :(得分:0)

var socket = io.connect('https://towd-server.herokuapp.com:8080');
var godzilla = document.getElementById('godzilla')

godzilla.onclick = charge

function charge()
{
    document.body.onfocus = roar
    console.log('chargin')
}
    
function roar()
{
    if(godzilla.files.length) alert('ROAR! FILES!')
    else alert('*empty wheeze*')
    document.body.onfocus = null
    console.log('depleted')
}
//source on findle http://jsfiddle.net/Shiboe/yuK3r/6/