作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在本地上传和读取文件,但我想使用自定义按钮而不是使用 HTML 输入来执行此操作。
<input type="file" id="my_file_input" />
我找到了这种方法,但我不想使用这个形状或这个按钮,我想使用一个 Material UI 凸起按钮来执行此功能以匹配其他站点按钮。
我也尝试了以下方法,但它没有用,因为当我点击按钮时没有任何反应。
<input type="file" id="my_file_input" style={{display:"none"}}/>
<label htmlFor="my_file_input">
<RaisedButton
label="Import from Excel"
labelColor="#FFFFFF"
backgroundColor="#01579b"
/>
</label>
我认为我应该在 RaisedButton
的 onClick
函数中手动执行上传/读取文件功能,但我没有找到这样做的方法。
那么这个问题在react中有没有其他的解决方案呢?
最佳答案
希望这段代码对您有所帮助。我们可以通过两种方式解决它。
<div>
<input type="file" hidden ref={this.inputReference} onChange={this.fileUploadInputChange} />
<button className="ui button" onClick={this.fileUploadAction}>
Image Upload
</button>
{this.state.fileUploadState}
</div>
constructor(props) {
super(props);
this.state={fileUploadState:""}
this.inputReference = React.createRef();
}
fileUploadAction = () =>this.inputReference.current.click();
fileUploadInputChange = (e) =>this.setState({fileUploadState:e.target.value});
<div>
<input id="fileButton" type="file" hidden />
<button onClick={this.fileUploadButton}>
Image Upload
</button>
{this.state.fileUploadState}
</div>
this.state = {fileUploadState:""}
fileUploadButton = () => {
document.getElementById('fileButton').click();
document.getElementById('fileButton').onchange = () =>{
this.setState({
fileUploadState:document.getElementById('fileButton').value
});
}
}
关于javascript - 如何使用自定义按钮在reactjs中读取和上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54673206/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!