Get contents of local folder in javascript (or ASP.NET VB)
Is it possible for a webpage to p开发者_开发技巧opup a open folder dialog, ask the user to select a folder, then show the contents of that folder in a list(or something) in the webpage. It won't write to the files, only read them. The webpage is hosted remotely.
Jonathan
In a word... No.
Requiring an ActiveX plug-in for your application is an invitation to utter failure. Unless you are writing a very target-specific app for an intranet where you control the client configuration, this is just a horrible idea.
There are strict limitations on what a web-based application can do, and this is one of them. What are you trying to accomplish? Perhaps there's a way to do it with a standard file upload dialog? Or WebDAV?
I wrote this small piece of code that shows a list of files given a folder name. It is written using VBScript so will work only on IE and FireFox(and maybe only on windows). But it is worth a look
<HTML>
<HEAD>
<SCRIPT LANGUAGE='VBSCRIPT'>
Sub showfiles()
On Error Resume Next
Dim fso, folder, files, sFolder, path
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = Document.getElementById("fdr").value
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
For each folderIdx In files
mydiv.innerhtml=mydiv.innerhtml & "<BR/> " & folderIdx.Name
Next
end sub
</SCRIPT>
</HEAD>
<BODY>
<INPUT id="fdr" TYPE="TEXT" VALUE="C:\" />
<INPUT TYPE="BUTTON" ONCLICK="showfiles()" value="show files" />
<DIV id="mydiv"></DIV>
</BODY>
</HTML>
You can use a Signed Java Applet, it is a fairly common solution. It works across browsers and platforms. All the user would have to do accept your certificate once and have the java runtime installed.
Alternatively you can write a browser plugin.
It'll make you (or your usesr) jump through a thousand hoops, but the ActiveX File System Object can possibly be used to do what you want...
http://msdn.microsoft.com/en-us/library/bkx696eh%28VS.85%29.aspx
edit -- added "possibly"
Edited :- Have a look at this. May be a workaround.
There is no way to do it with JavaScript, as it has no way of working with the operating system.
There is, however, a way to do it with VBScript (ASP.NET), but IE will throw a security warning to the user before allowing the code to execute only if their security level is less than Medium-Low.
If you're trying to access local files over the Internet, your best bet (outside of finding a vulnerability and getting access that way, i.e. the bad way) is by using Java or Flash.
If you still want the code (for ASP/VBscript):
Dim FileSystem
Set FileSystem = GetObject("Scripting.FileSystemObject")
If Err.Number <> 0 Then
MsgBox("Error setting FileSystem object; check WSH version.")
WScript.Quit(0)
End If
Dim Folder
Set Folder = FileSystem.GetFolder("folder_name")
If Err.Number <> 0 Then
MsgBox("Error getting folder.")
WScript.Quit(1)
End If
-Carlos Nunez
精彩评论