Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi,

I would like to check the whether the .txt file exists or not in the particular folder using Javascript.If text file exists Then I would to like to reload the page.

Please send me the sample code for this scenario.

Thanks!
Posted
Comments
Richard Deeming 15-Jan-16 15:08pm    
Where is the file located? If it's on the client, then you cannot access it from Javascript running in a browser.
priya9826 19-Jan-16 12:30pm    
Text file is located on the server Side.
Richard Deeming 19-Jan-16 12:35pm    
Then Ryan's answer (Solution 1) is correct - you need to call some code running on the server to check the file.

As I mentioned in your other related question, don't pass the path of the file into the service; have it hard-coded or stored in a server-side configuration file. You don't want hackers to be able to check random files anywhere on the server! :)
priya9826 19-Jan-16 12:40pm    
Could you please send me the sample code which use jQuery's .ajax() method to Check whether .txt file exists or not in the folder.If file exists reload the page automatically?
Richard Deeming 19-Jan-16 12:45pm    
The code depends on whether the file can be downloaded, and what server-side language you're using.

If you can download the file, then something like this would work:
$.ajax("path/to/the/file.txt").done(function(){ /* File exists */ }).fail(function(){ /* File doesn't exist */ });

If you can't download the file, then you'll need to write some server-side code to check whether it exists.

1 solution

As Richard mentioned, JavaScript cannot access the local client's PC's file system. That would be a huge security risk.

However, if you are referring to the server then you can use jQuery's .ajax() method, jQuery.ajax() | jQuery API Documentation[^] to call a webservice that checks the file system on the server and returns an appropriate response.

Note, if you are checking the server you'll need to make sure the account shown as the identity of the application pool in IIS will need permissions to the file system or else it checking will always return false.
 
Share this answer
 
Comments
priya9826 19-Jan-16 12:39pm    
Could you please send me the sample code which use jQuery's .ajax() method to Check whether .txt file exists or not in the folder.If file exists reload the page automatically?
ZurdoDev 19-Jan-16 13:33pm    
No, that is way too much code. We'll gladly help you when you get stuck if you'll come back and ask a new specific question and show where you are stuck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900