我正在尝试使用带有imgur api(react)的html表单上传图像。
我在注册 api 时选择了带有回调 URL 的 OAuth 2 授权。
问题是 api 不会出现错误 429(有时是 net::ERR_HTTP2_PROTOCOL_ERROR)。
这是代码
const imageUpload = (e) => {
console.log("called");
var fileIn = e.target;
var file = fileIn.files[0];
if (file && file.size < 5e6) {
const formData = new FormData();
formData.append("image", file);
fetch("https://api.imgur.com/3/image", {
method: "POST",
headers: {
Authorization: "Client-ID //my client Id",
Accept: "application/json",
},
body: formData,
})
.then((response) => response.json())
.then((response) => {
e.preventDefault();
console.log(response);
console.log(response.data.link);
url_in = response.data.link;
});
} else {
console.error("oversized file");
}
}
这是输入标签代码
<input type="file" name="image" id="upload" onChange={imageUpload}></input>
我只需要上传图片的网址