tags:

views:

104

answers:

7

consider a path="~/uploads/" how can i get the no. of files in it through javascript

+2  A: 

You won't be able to do that using JavaScript alone. Use a server side language to fetch the number of files.

rahul
ok but is it possible like on pageload i will calculate no of files and pass them as an argument in javascript function
sumit
A: 

You can execute an ajax-request to server script that returns file count.

silent
+7  A: 

With javascript you cant access local file system because of security reasons.

But it looks like you're trying to access server-side folder. So you should access the folder you want by your server side application, and register variables in client script into your page.

For example it looks like you want to get the number of files in your uploads folder, and you're using ASP.NET. Here is a little snippet that registers the number of files in a server folder :

// at page load : 
string myScript = "<script>var numberOfFiles = " + GetNumberOfFiles() + ";</script>";
Page.ClientScript.RegisterStartupScript(typeof(myPage), "myScriptKey", myScript);

The code abowe will render the script that sets the number of files to a client side variable. So you can use numberOfFiles variables in your page's client side functions to do your stuff.

Canavar
So you're assuming `~/uploads/` is a unix home folder. Valid assumption, as there is no data...
Kobi
no no,, i m using asp.net
sumit
Is this server side JScript then? Or are you talking client side JavaScript? If the latter, then you are out of luck.
David Dorward
+2  A: 

You cannot, directly. A path like ~/uploads/ exists on the server side, but JavaScript on the client side does not "see" files and folders, just urls.
Your best option is to use AJAX, and solve this problem on the server side.

Kobi
A: 

There are no built in file system functions in JavaScript., so you would need to be using a JavaScript runtime environment that provided access to the filesystem.

The specifics would then depend on the API provided in said runtime.

The standard web browser security context provides no such API.

David Dorward
A: 

Hope the following links help your query

http://stackoverflow.com/questions/1027571/javascript-read-files-in-folder

A.m.a.L
+1  A: 

I believe "no." means, "number" - sumit is looking for the number of files in a folder (but was merely being too lazy to type out the entire word, "n-u-m-b-e-r" - hey, great, half a nanosecond of laziness lead to a bunch of people wondering, "what the heck is this idiot asking for?"). Lesson to be learned here, sumit: Spell it out. Don't assume anything.

Now, on to the clues: the tilde (~)... that's not usually a Windows convention. Folders can be created named, "~" but it isn't usually seen in Windows. I have seen it used more in Unix/Linux. Another lesson: specify the target platform, especially if you're looking for server/client side help.

Rules For Any platform: Your JS running within the context of a browser will NOT be able to see any file systems. Your JS running within the context of the operating system WILL be able to see the file system (depending on the OS).

Assuming Windows, running on the server or locally, on a client - Google: Javascript FSO There are plenty of scripts out there that work with the File System Object in Windows. FSO is really the thing here that exposes the file system to JS. JS, by itself, won't be able to see it.

Assuming Unix/Linux/OS X - Aw, hell... if you use Unix/Linux/OS X, use PERL, C or pipe ls to grep. Then, output the data to a file in webspace, where your JS can read it. Stick the script on a timer (if you don't need a 'live' number), or put it in webspace and treat it like a CGI script.

inked