Skip to content Skip to sidebar Skip to footer

How To Check If A Url Is A File Or Folder In Javascript?

In javascript/jquery, given a string of a url, how can I check if it is a url to a file or a directory? Thanks

Solution 1:

You can't, because there's no difference in HTTP.

A URL is neither a "file" nor a "directory." It's a resource. When requesting that resource, the server responds with, well, a response. That response consists of headers and content.

A header (such as content-disposition) may indicate that the response should be treated as a file by the consuming client. But in and of itself it's not a "file" because HTTP is not a file system.

And any resource can return any response that the server wants. For example, you might request http://www.something.com and expect not to get a file because you didn't ask for one. But it can still return one. Or, even if you ask for index.html you might not get a "file" called "index.html" but instead some other response.

Even if you ask for a "directory" from your point of view, the server is still responding with headers and content. That content may take the form of a directory listing, but it's indistinguishable from any other successful response aside from parsing the content itself.

If you're looking for something which the server has indicated is a "file" then you're looking for the content-disposition header in the response and you'll want to parse out the value of that header. Other than that one case, I'd suspect that whatever need you have to know if it's a "file" or a "directory" is a symptom of a design problem in whatever you're trying to do, since the question itself is moot in HTTP.

Solution 2:

Like David said, you can't really tell. But if you want to find out if the last part of a url has a '.' in it (maybe that's what you mean by a "file"?), this might work:

functionisFile(pathname) {
    return pathname.split('/').pop().indexOf('.') > -1;
}

functionisDir(pathname) { return !isFile(pathname); }

console.log(isFile(document.location.pathname));

Solution 3:

Just to update with a version that works with a string, you can use the built in URL function in JavaScript (unless you are in <IE11). As those who have come before me have mentioned, it doesn't work in many cases, this is just a rough hack to check for a . in the filename of a string url.

constcheckIfFile = (url) => {
  url = newURL(url);
  return url.pathname.split('/').pop().indexOf('.') > 0;
}

const urls = [
  "http://example.com",
  "http://example.com?v=1",
  "http://example.com/no",
  "http://example.com/no?no=3.1",
  "http://example.com/yes.jpg?yes=3.1",
  "http://example.com/yes.jpg",
  "http://example.com/maybe.someone.did.this/yes.jpg",
  "http://example.com/maybe.someone.did.this/",
];

urls.forEach(url => {
    console.log("The url " + url + " is " + (checkIfFile(url) ? "true" : "false"));
})

Outputs the following:

"The url http://example.com is false""The url http://example.com?v=1 is false""The url http://example.com/no is false""The url http://example.com/no?no=3.1 is false""The url http://example.com/yes.jpg?yes=3.1 is true""The url http://example.com/yes.jpg is true""The url http://example.com/maybe.someone.did.this/yes.jpg is true""The url http://example.com/maybe.someone.did.this/ is false"

Post a Comment for "How To Check If A Url Is A File Or Folder In Javascript?"