如何在同一窗口中选择文件或文件夹?根据
MSDN,
Shell.BrowseForFolder
方法创建一个对话框,使用户能够选择一个文件夹,然后返回所选文件夹的
Folder
对象。
因此,要浏览文件,我们需要使用另一种方法。存在本地的
<input ...>
标签与
type="file"
属性。然而,使用它带来的缺点比优点更多(请参见下面的第2个解决方案)。
在下一个解决方案中,通过使用
HtmlDlgHelper
对象 - 一个鲜为人知且文档质量较差的对象,可以实现浏览文件。与上述
<input ...>
标签相比,这个解决方案提供了定义初始位置和文件类型掩码的功能。
<HTML>
<HEAD>
<Title>File/Folder Copy B</Title>
<!-- Browse for
- file: based on the HTMLDlgHelper class
registered via C:\Windows\System32\mshtmled.dll
(.OpenFileDlg returns string)
- folder: based on the Shell.BrowseForFolder method
(returns the selected folder's Folder object)
-->
<HTA:APPLICATION
ID=""
APPLICATIONNAME="28632270ym"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"
>
<script language="VBScript">
''''''''''''''''''''''''''
''' Global State Variables
''''''''''''''''''''''''''
Dim sStartPath, sStartFile, sFilter, sCaption
sStartPath = "D:\Remote"
sStartFile = sStartPath & "\nul"
sFilter = "All Files (*.*)|*.*|" _
& "VBScript (*.vbs;*.vbc)|*.vbs;*.vbc|" _
& "HTML (*.htm;*.html;*.hta)|*.htm;*.html;*.hta|"
sCaption = "Choose a File:"
Sub Window_Onload
self.Focus()
self.moveTo 1, 1
self.ResizeTo 760,400
End Sub
Sub BrForFolder
sBFF = PickFolder(sStartPath)
If not sBFF = "" Then
document.Copy_To_PC.txtfile.value = sBFF
document.Copy_To_PC.OKbuton.value = "Treat folder"
End If
End Sub
Sub BrForFile_onclick
sBFF = Dlg.OpenFileDlg( CStr(sStartFile), , CStr(sFilter), CStr(sCaption))
If not sBFF = "" Then
document.Copy_To_PC.txtfile.value = sBFF
document.Copy_To_PC.OKbuton.value = "Treat file"
End If
End Sub
Function PickFolder(sStartPath)
Dim shell, oFldr
Set shell = CreateObject("Shell.Application")
Set oFldr = shell.BrowseForFolder(0, "Choose a folder:" _
, &H0001 + &H0004 + &H0010 + &H0020, sStartPath)
'See MSDN "BROWSEINFO structure" for constants
If (Not oFldr Is Nothing) Then
PickFolder = oFldr.Self.Path
Else
PickFolder = ""
End If
Set shell = Nothing
Set oFldr = Nothing
End Function
Sub DoFileOK()
MsgBox document.Copy_To_PC.OKbuton.value _
& " """ & document.Copy_To_PC.txtfile.value & """"
document.Copy_To_PC.txtfile.value = ""
document.Copy_To_PC.OKbuton.value = "Undefined"
End Sub
</script>
</HEAD><BODY>
<OBJECT id=Dlg classid="CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B"
width=0 height=0>
</OBJECT>
<FORM name="Copy_To_PC">
<p><b><font size="3">Please select a file or folder.</font></b></p>
<input style="width:600px;" type="text" id="txtfile" name="txtfile" />
<br>
<input type="button" value="Browse for File..." id="BrForFile">
or
<input type="button" value="Browse for Folder..." onClick=BrForFolder()>
<br>
<br>Action
<input type="button" value="Undefined" id="OKbuton" onClick=DoFileOK()>
<br>
<br>Quit:
<input type="button" value="All done" onClick=self.window.close()>
</FORM></BODY></HTML>
第二种解决方案:
<html>
<head>
<Title>File/Folder Copy A</Title>
<!-- based on <input type="file"> of zero width -->
<HTA:APPLICATION
ID=""
APPLICATIONNAME="28632270my"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"
>
<script language = "VBScript">
''''''''''''''''''''''''''
''' Global State Variables
''''''''''''''''''''''''''
sStartPath = "D:\Remote"
sFilter = "application/pdf,text/plain"
Sub Window_Onload
self.Focus()
self.moveTo 1, 1
self.ResizeTo 960,400
End Sub
Sub BrForFolder()
sBFF = PickFolder(sStartPath)
If sBFF = "" Then
'do not overwrite .txtfile.value with an empty string
Else
Copy_To_PC.txtfile.value = sBFF
Copy_To_PC.OKbuton.value = "Treat folder"
End If
'NOT possible (security reason): Copy_To_PC.fn_file.value = ""
End Sub
Sub BrForFile()
'??? how to know whether a user has pressed 'Open' or 'Cancel' ???
'https://dev59.com/NW445IYBdhLWcg3w7ug0#4628563
'The result of the file dialog is not exposed to the browser.
'
'??? how to set initial working directory???
' and file types??? (MSIE does not care about the accept attribute?)
sBFF = Copy_To_PC.fn_file.value
If sBFF = "" Then
'do not overwrite .txtfile.value with an empty string
Else
Copy_To_PC.txtfile.value = sBFF
Copy_To_PC.OKbuton.value = "Treat file"
End If
End Sub
Function PickFolder(sStartPath)
Dim shell : Set shell = CreateObject("Shell.Application")
Dim oFldr : Set oFldr = shell.BrowseForFolder(0, "Choose a folder:" _
, &H0001 + &H0004 + &H0010 + &H0020, sStartPath)
'See MSDN "BROWSEINFO structure" for constants
'Shell.BrowseForFolder method: Creates a dialog box that
' enables the user to select a folder and then returns
' the selected folder's Folder object.
If (Not oFldr Is Nothing) Then
PickFolder = oFldr.Self.Path
Else
PickFolder = ""
End If
Set shell = Nothing
Set oFldr = Nothing
End Function
Sub DoFileOK()
MsgBox Copy_To_PC.OKbuton.value _
& " """ & Copy_To_PC.txtfile.value & """"
Copy_To_PC.txtfile.value = ""
Copy_To_PC.OKbuton.value = "Undefined"
End Sub
</script>
</head><body><form name="Copy_To_PC">
<p><b><font size="3">Please select a file or folder.</font></b></p>
<input style="width:600px;" type="text" id="txtfile" name="txtfile" />
<input style="width:0px;" type="file" id="fn_file" name="fn_file"
accept=sFilter onChange=BrForFile() onBlur=BrForFile()>
or
<input type="button" value="...for Folder..." onClick=BrForFolder()>
<br>
<br>Action
<input type="button" value="Undefined" id="OKbuton" onClick=DoFileOK()>
<br>
<br>Quit:
<input type="button" value="All done" onClick=self.window.close()>
</form></body></html>
BrowseForFolder()
对话框中选择文件而不是文件夹时发生的。不确定如何修复它(甚至是否可以完全修复)。要传递根路径,必须将其添加为最后一个参数,即在&H400
之后。 - Tomalak.rtm
文件(如果存在的话)? - Root Loop