如何使用JavaScript确定目录中是否存在文件? [英] How to use JavaScript to determine whether a file exists in a directory?

查看:72
本文介绍了如何使用JavaScript确定目录中是否存在文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

如果在服务器上,则可以创建

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
    }
  }
});

这篇关于如何使用JavaScript确定目录中是否存在文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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