2 回答

TA贡献2041条经验 获得超4个赞
您可以发出 HEAD 请求以查看 URL 是否存在。也许是这样的。
async function exists(url) {
const result = await fetch(url, { method: 'HEAD' });
return result.ok;
}

TA贡献1811条经验 获得超6个赞
import React from "react";
import "./styles.css";
export default class App extends React.Component {
state = { isFileExists: false };
componentDidMount() {
this.checkFIle(window.location.origin + "/files/FL_1.zip")
.then(() => {
this.setState({ isFileExists: true });
})
.catch(err => {
this.setState({ isFileExists: false });
});
}
checkFIle = async url => {
return fetch(url, { method: "HEAD" });
};
render() {
const { isFileExists } = this.state;
return (
<div className="App">
{isFileExists && <p>File Exists</p>}
{!isFileExists && <p>File Not Exists</p>}
</div>
);
}
}
查看以下网址中的代码示例:
https://codesandbox.io/s/loving-napier-dpli5
添加回答
举报