[英]On click of image icon, refer the input type file function in REACT.js
Here I have image icon, with edit icon of it. 这里有图像图标,带有编辑图标。 And when I click on edit icon, image input type file is triggered and picking the file same!
当我单击编辑图标时,图像输入类型文件被触发并选择相同的文件!
But I count get the event.target.file here... 但我指望在这里获取event.target.file ...
<span className='fa fa-edit edit-icon'
onClick={(e)=>{this.onChangeFile.click(e)}}> </span>
<input type="file" id="file"
ref={(ref) => this.onChangeFile = ref}
style={{display: "none"}}/>
method: 方法:
But I count get the event.target.file here... 但我指望在这里获取event.target.file ...
onChangeFile =(event)=> {
console.log('event.target.files', event.target.files)
event.stopPropagation();
event.preventDefault();
var file = event.target.files[0];
console.log('file.....:', file);
this.setState({file});
}
Here I am looking for the file path in log. 在这里,我正在寻找日志中的文件路径。 currenlty it is not calling the method.
当前它没有调用该方法。
There are several issues in your code: 您的代码中存在几个问题:
onChangeFile
handler will be erased by the ref callback ref={(ref) => this.onChangeFile = ref}
onChangeFile
处理程序将被ref回调ref={(ref) => this.onChangeFile = ref}
擦除。 onchange
event of your input. onchange
事件上绑定任何事件处理程序。 It seems that what you are trying to do is to replace the ui of the file browser with a nice edit icon. 看来您想要做的是用一个不错的编辑图标替换文件浏览器的ui。
Html label
element and its for
attribute are there to help you. HTML
label
元素及其for
属性可以为您提供帮助。
The following snippet is plain html/js (no react) but will work in your component if you change the plain html for
attribute for its React counter part htmlFor
and if you bind the onChangeFile
handler the jsx way that is 以下代码段是纯html / js(无反应),但如果您更改其React计数器部分
htmlFor
的纯html for
属性,并且如果您以jsx方式绑定onChangeFile
处理函数,则将在您的组件中工作
<input
type="file"
id="file"
onChange={this.onChangeFile}
style={{display: "none"}}
/>
Instead of document.getElementById('file').addEventListener('change', onChangeFile);
代替
document.getElementById('file').addEventListener('change', onChangeFile);
The refs are not needed here. 此处不需要裁判。
So to sum everything up: 所以总结一下:
label
element. label
元素中。 htmlFor
attribute to the same value as the file input's id
attribute. htmlFor
属性设置为与文件输入的id
属性相同的值。 That's it, the icon will act as your hidden input should. 就是这样,该图标将充当您的隐藏输入应有的角色。
onChangeFile = (event)=> { console.log('event.target.files[0]', event.target.files[0]) event.stopPropagation(); event.preventDefault(); var file = event.target.files[0]; console.log('file.....:', file); } document.getElementById('file').addEventListener('change', onChangeFile);
.edit-icon { display: inline-block; width: 20px; height: 20px; background-color: #EEAA11; }
<label for="file"><span class='fa fa-edit edit-icon'> </span><label> <input type="file" id="file" style="display: none;"/>
<input type="file" id="file" style={{display: "none"}}
onChange={(e) => this.onChangeFile(e)}/>
<label htmlFor="file" >
<span className='fa fa-edit edit-icon'> </span>
</label>
onChangeFile = (event)=> {
console.log('event.target.files[0]', event.target)
}
Here I just bind the lable with input element. 在这里,我只是将标签与输入元素绑定在一起。 Made
display:none
for input. display:none
输入。 And it works for me. 它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.