<input
id="file"
ref={(el) => {
this.input = el;
}}
type="file"
accept="image/*"
onChange={this.HandleImageChange}
></input>
HandleImageChange = (e) => {
//处理图片
const file = e.target.files[0];
console.log(e, file);
var reader = new FileReader();
reader.onload = function (e) {
// console.log(reader.result); //或者 e.target.result都是一样的,都是base64码
};
reader.readAsDataURL(file);
var myHeaders = new Headers();
myHeaders.append(
"Authorization",
`Bearer ${localStorage.getItem("token")}`
);
var formdata = new FormData();
formdata.append(`${file.name}`, file);
var requestOptions = {
method: "POST",
headers: myHeaders,
body: formdata,
redirect: "follow",
};
fetch(`${this.state.requestUrl}`, requestOptions)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log("error", error));
};