JS获取检查文件是否存在而不下载内容
JS fetch check if file present without downloading content
如何使用 JS 提取 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
}
});
如何使用 JS 提取 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
}
});