0

I'm new to JS,can anyone tell me how to check if the file exists in the mentioned path in js ? In the back end i am using ROR .

Ex: In the UI I enter a path where a particular file need to be exported . Before the export function takes place a check needs to be done.

Export function should not take place if the file already exists and an alert message should be displayed to the user .

Thanks.

  • 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) – Mihai Matei Jun 16 '15 at 12:20
  • Where are you looking to see if the file exist ? JS is only client side.. And has no acces to users filesystem at that! – Pogrindis Jun 16 '15 at 12:20
  • @Pogrindis — It's asking about the server not the local filesystem – Quentin Jun 16 '15 at 12:20
  • There's only one way to implement this: Once a path you want to check is entered, you send some Ajax-request, test it there via RoR and send response back – Yang Jun 16 '15 at 12:20
  • In that case @MateiMihai is correct. – Pogrindis Jun 16 '15 at 12:21
  • 1
    This is better handled by the server side code that tries to create the file returning an error to the client if the file already exists. Doing the test client side is probably more effort and may introduce a race condition. – Quentin Jun 16 '15 at 12:21
  • That point needs to be highlighted a lot more @Quentin ! – Pogrindis Jun 16 '15 at 12:22
  • *JavaScript* and *path* can have many different meanings nowadays. Perhaps you should edit your question and explain your environment (web browser, Node, browser add-on...), context (local file? remote URL?) and code (regular file upload, FileReader API...?) – Álvaro González Jun 16 '15 at 12:23
  • @ÁlvaroG.Vicario here the path is file path – Priyanka Chabbi Jun 16 '15 at 12:26
  • @DavidY how do i send an response back from server to Js even if i go for ajax call – Priyanka Chabbi Jun 16 '15 at 12:27
  • You would simply do `puts 1` if file exists, otherwise you would do `puts 0`. Then there's a method in jquery's ajax, which need to be defined by you. It's called `success` and looks as : `$.ajax({ url : "/controller/action", success : function(response){ if (response == "1"){ alert('File exists') } } })` – Yang Jun 16 '15 at 12:38
  • @DavidY in response i am getting the whole of the html page not just the value . – Priyanka Chabbi Jun 17 '15 at 06:02

1 Answers1

0

By adding render :test => "Value you want to pass" at the end of ror function and calling success: function(result) will fetch the value passed by the function in the ajax call.