如何使用 JS fetch API 检查文件是否存在于 URL 中,而不下载文件(如果存在)?本质上,我只想检索状态代码而不是内容。
我不想下载文件,因为它可能很大,下载整个文件会浪费带宽,因为我不关心内容。
最佳答案
这可以通过将请求方法设置为 HEAD 来完成,requests the headers only .
fetch("http://localhost:3000/file",
{ method: "HEAD" }
).then((res) => {
if (res.ok) {
// file is present at URL
} else {
// file is not present at URL
}
});
关于javascript - JS获取检查文件是否存在而不下载内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68595209/