Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中检查文件是否存在?_Javascript_Jquery - Fatal编程技术网

如何在javascript中检查文件是否存在?

如何在javascript中检查文件是否存在?,javascript,jquery,Javascript,Jquery,您能告诉我如何检查javascript中是否存在文件吗?实际上我使用的是Fileserver.js。运行时,它会创建一个名为“a.txt”的文件。当我再次运行它时,它会创建:a.txt(1)[a.txt(2)…]。我想检查该文件是否存在,以便它可以询问用户是否要覆盖该文件(如果该文件存在) 我在谷歌上搜索它,但每次都是假的。 您可以使用对服务器的ajax请求来确定文件是否存在 演示: 希望能解决您的问题。这样行吗 function UrlExists(url) { var http =

您能告诉我如何检查javascript中是否存在文件吗?实际上我使用的是
Fileserver.js
。运行时,它会创建一个名为
“a.txt”
的文件。当我再次运行它时,它会创建:
a.txt(1)
[
a.txt(2)
…]。我想检查该文件是否存在,以便它可以询问用户是否要覆盖该文件(如果该文件存在)

我在谷歌上搜索它,但每次都是假的。

您可以使用对服务器的ajax请求来确定文件是否存在

演示:

希望能解决您的问题。

这样行吗

function UrlExists(url)
{
    var http = new XMLHttpRequest();
    http.open('GET', url, false);
    http.send();
    if(http.readyState==4){  
    if(http.status==200)s+=" exists."; 
    else if(http.status==404)s+=" doesn't exist.";  
    else s+="";//any other status    
}

可能重复,我检查stackoverflow请不要工作我检查此..请检查此fiddle在url中写入的内容:“”,它正在下载路径C:\Users\asd\Downloads是否要删除保存在本地的文件?如果是这样,您应该尝试html5文件api。但是html5在browser.jsfiddle.net/zrnQR/4中存在兼容性问题–它不起作用..我想知道您能提供什么fiddle@user2535959-您只能在与主机页相同的域中使用Ajax,因此类似的解决方案只能在相同的域中工作(这称为同源限制)。您创建的JSFIDLE不能在您的域中使用您的文件,因为它位于不同的域中。这意味着我只需要在我的计算机上进行检查吗?@jfriend00您能否提供一个简单的示例,使用filesaver.js在您的pc上工作大约需要5分钟
$.ajax({
    url: 'http://www.yoururl.com/path/file.txt',
    type: 'GET',
    error: function()
    {
        //not exists
    },
    success: function()
    {
        // exists
    }
});
function UrlExists(url)
{
    var http = new XMLHttpRequest();
    http.open('GET', url, false);
    http.send();
    if(http.readyState==4){  
    if(http.status==200)s+=" exists."; 
    else if(http.status==404)s+=" doesn't exist.";  
    else s+="";//any other status    
}