如何检查 url 是 javascript 中的文件还是文件夹? [英] How to check if a url is a file or folder in javascript?

查看:65
本文介绍了如何检查 url 是 javascript 中的文件还是文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript/jquery中,给定一个url字符串,我如何检查它是文件还是目录的url?

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

谢谢

推荐答案

你不能,因为 HTTP 没有区别.

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

URL 既不是文件"也不是目录".这是一个资源.当请求该资源时,服务器响应,嗯,一个响应.该响应由标题和内容组成.

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.

标头(例如content-disposition)可能表明响应应该被消费客户端处理作为一个文件.但它本身并不是一个文件",因为 HTTP 不是一个文件系统.

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.

并且任何资源都可以返回服务器想要的任何响应.例如,您可能请求 http://www.something.com 并期望不会获得文件,因为您没有请求.但它仍然可以返回一个.或者,即使您请求 index.html,您也可能不会得到名为index.html"的文件",而是一些其他响应.

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.

如果您正在寻找服务器已指示为文件"的内容,那么您正在寻找响应中的 content-disposition 标头,并且您需要解析出该标头的值.除了那一种情况,我怀疑你需要知道它是文件"还是目录"是你尝试做的任何事情的设计问题的症状,因为问题本身没有实际意义在 HTTP 中.

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.

这篇关于如何检查 url 是 javascript 中的文件还是文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆