0

首先抱歉标题不好,如果有人可以更好地命名这个问题,请做。

我有这个 HTML 文件:

<html>
  <form action='' method='post'>
    <input id='inbox' type='file' />
    <input id='outbox' type='button' onclick='alert(inbox.value);' 
      value='submit' />
  </form>
</html>

当我用 IE 打开它,然后浏览文件并单击“提交”按钮时,我会收到所选文件的完整路径的警报。但是,如果我尝试使用 VBScript 编写相同的脚本:

Set IE = CreateObject("InternetExplorer.Application")
IE.Offline = True
IE.Navigate "about:blank"

Do
Loop While IE.Busy

html = "<html>" &_
       "  <form action='' method='post'>" &_
       "    <input id='inbox' type='file' />" &_
       "    <input id='outbox' type='button' onclick='alert(inbox.value);'" &_
       "      value='submit' />" &_
       "  </form>" &_
       "</html>"

IE.Document.write html
IE.Width = 0
IE.Height = 0
IE.Document.All("inbox").Click
IE.Document.All("outbox").Click

我收到了臭名昭著的c:\fakepath\file.ext消息。

有谁知道为什么会这样甚至更好地克服它?

4

2 回答 2

2

After reading your last comment - "I want file open dialog that will return full path and would work on XP and 7", and as I see you want to call this dialog in WSH environment, then you can use CommonDialog Control. This control is disabled in Windows 8, but works fine up to Windows 7, as you intend. Need to note that on 64-bit Windows by default .VBS files executed as 64-bit process, but that control is 32-bit, for that reason I include in my example code and function that restart the script as 32-bit process.

Call Force32bit

With CreateObject("MSComDlg.CommonDialog")
    .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    .InitDir = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
    .MaxFileSize = 256
    .Flags = &H80000 + &H4 + &H8
    .ShowOpen
    If Len(.FileName) Then
        WScript.Echo .FileName
    Else
        WScript.Echo "Canceled"
    End If
End With

Sub Force32bit()
    Dim sWinDir, sSys64, sSys32, oShell
    Set oShell = CreateObject("WScript.Shell")
    sWinDir = oShell.ExpandEnvironmentStrings("%WinDir%")
    With CreateObject("Scripting.FileSystemObject")
        sSys64 = .BuildPath(sWinDir, "SysWOW64")
        If Not .FolderExists(sSys64) Then Exit Sub
        sSys32 = .BuildPath(sWinDir, "System32")
        If sSys32 = WScript.Path Then
            oShell.CurrentDirectory = sSys64
            oShell.Run "wscript.exe " & Chr(34) & _
            WScript.ScriptFullName & Chr(34), 1, False
            WScript.Quit
        End If
    End With
End Sub
于 2013-02-18T07:43:17.363 回答
0

这是 IE 中的安全设置,您可以通过将 html 启动为像这样的 hta 来规避它

Set wShell=CreateObject("WScript.Shell")
Set oExec=wShell.Exec("mshta.exe ""about:<input type=file id=FILE><script>FILE.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);</script>""")
MsgBox oExec.StdOut.ReadAll
于 2013-02-17T18:19:44.150 回答