最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS fetch check whether file exists without downloading content - Stack Overflow

programmeradmin0浏览0评论

How do I check whether a file is present at a URL using the JS fetch API, without downloading the file if it is present? Essentially, I just want to retrieve the status code but not the content.

I don't want to download the file because it may be large, and downloading the whole thing would waste bandwidth since I don't care about the contents.

How do I check whether a file is present at a URL using the JS fetch API, without downloading the file if it is present? Essentially, I just want to retrieve the status code but not the content.

I don't want to download the file because it may be large, and downloading the whole thing would waste bandwidth since I don't care about the contents.

Share Improve this question edited Jan 10, 2024 at 18:23 The Bic Pen asked Jul 30, 2021 at 17:50 The Bic PenThe Bic Pen 1,1478 silver badges29 bronze badges 2
  • 1 Does this answer your question? How do I check if file exists in jQuery or pure JavaScript? – kmoser Commented Jul 30, 2021 at 17:52
  • Yes. I found that link after filling in the details of the post, and based my answer off of that – The Bic Pen Commented Jul 30, 2021 at 17:56
Add a comment  | 

1 Answer 1

Reset to default 20

Set the request method to HEAD, which 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
      }
    });
发布评论

评论列表(0)

  1. 暂无评论