3

Can you please tell me how to check if a file exists or not in javascript? Actually I'm using Fileserver.js. When run, it creates a file named "a.txt". When I run it again, it creates: a.txt(1) [a.txt(2)...]. I want to check if the file exists so it can ask the user if they want to overwrite the file (if the file exists).

http://jsfiddle.net/zrnQR/1/

$(function(){
    var blob = new Blob([ "iiiiiii" ], {
        type : "text/plain;charset=utf-8"
    });
    saveAs(blob, 'a,txt');
});

I googled to find it, but it's giving false every time.
http://jsfiddle.net/zrnQR/4/

Kevin Fegan
  • 1,292
  • 1
  • 16
  • 29
Shruti
  • 1,554
  • 8
  • 29
  • 66
  • possible duplicate of [How do I check if file exists in jQuery or JavaScript?](http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascript) and [Check if a file exists locally using javascript ONLY](http://stackoverflow.com/questions/5115141/check-if-a-file-exists-locally-using-javascript-only) – h2ooooooo Jun 25 '14 at 06:14
  • i check the stackoverflow please http://jsfiddle.net/zrnQR/4/ – Shruti Jun 25 '14 at 06:23

2 Answers2

3

You can use the ajax request to server to find out if the file exists.

Demo:

$.ajax({
    url: 'http://www.yoururl.com/path/file.txt',
    type: 'GET',
    error: function()
    {
        //not exists
    },
    success: function()
    {
        // exists
    }
});

Hope to solve your problem.

Tyler.z.yang
  • 2,402
  • 1
  • 18
  • 31
0

Will this do?

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    
}
meeming
  • 88
  • 13
  • jsfiddle.net/zrnQR/4 – it is not working ..I checking anwer – Shruti Jun 25 '14 at 06:33
  • can you provide fiddle – Shruti Jun 25 '14 at 06:33
  • @user2535959 - you can only use Ajax to the same domain as the host page so a solution like this has to work only on the same domain (this is called same-origin restrictions). No jsFiddle you create will work with your file on your domain because it's on a different domain. – jfriend00 Jun 25 '14 at 06:35
  • Mean I need only check on my computer ? – Shruti Jun 25 '14 at 06:36
  • @jfriend00 can you provinde a simple example which is working on your pc using filesaver.js it will take around 5 min – Shruti Jun 25 '14 at 06:37
  • @user2535959 - if you're trying to check for the existence of a file on your own PC, you can't do that (for security reasons). These ajax-based solutions (none of which I wrote - I'm just commenting here) only work to check for a file on the same server that hosts your web page. – jfriend00 Jun 25 '14 at 06:38
  • ok...can we check on server ? not on pC – Shruti Jun 25 '14 at 06:40
  • @user2535959 You can call a server side script (eg. PHP) using AJAX, and make the server code check it for you. – h2ooooooo Jun 25 '14 at 07:23