1 回答
TA贡献1850条经验 获得超11个赞
上传的文件无法显示,您需要<img />带src属性的标记
src可以通过读取以下内容来生成属性的值event.target.files[0]
这样的事情应该做:
import React, { Component } from "react";
export default class extends Component {
constructor(props) {
super(props);
this.state = {
selectedImage: null.
selectedImageContent: ""
}
}
onChange = (event) => {
const file = event.target.files[0];
const fileReader = new FileReader();
fileReader.onload = this.onFileLoad;
this.setState({
selectedImage: event.target.files[0]
})
};
onFileLoad = (e) => {
this.setState({
selectedImageContent: e.target.result
});
}
render() {
const { selectedImageContent } = this.state;
return (
<div>
<div>
{selectedImageContent && <img src={selectedImageContent} />}
</div>
<form onSubmit={this.onFormSubmit}>
<input type="file" name="someImage" accept="image/*" onChange={this.onChange} />
<button>Add This Car</button>
</form>
</div>
)
}
}
更多信息在这里
添加回答
举报