如何使用JavaScript确定目录中是否存在文件?

问题描述:

如何使用javascript确定目录中是否存在文件?

How to use javascript to determine whether a file exists in a directory?

如果在服务器上,则可以创建 404 (未找到)或

If it's on the server, you could make an HTTP HEAD request via Ajax, and look if the HTTP Status Code is 404 (Not found) or 200 (Ok).

使用jQuery的示例:

An example using jQuery:

$.ajax({
  type: 'HEAD',
  url: 'somefile.ext',
  complete: function (xhr){
    if (xhr.status == 404){
      alert(xhr.statusText); // Not found
    }
  }
});