我正在尝试制作一个 Windows 窗体程序来自动将文件上传到网站。
该网站有一个来自 input type="file">
的“浏览...”按钮标签,我在上面 InvokeMember("click")
触发文件选择器,但我无法与之交互。
我不熟悉那个控件。我试过 SendKeys.Send("quotes.html") : SendKeys.Send(Chr(13))
,完全没有成功。 SendKeys.Send
命令仅在我手动关闭文件选择器后执行。
有没有人知道如何在这个 <input type="file">
中选择一个文件?使用 VB.NET 进行控制?
编辑 #1/08.04.2015 13:00/:
我上传文件的网络服务是第 3 方应用程序,我无法修改。我只想与它交互,以便我的应用程序可以通过与出现的弹出式文件上传表单交互来自动上传文件,但我不知道该怎么做。
此过程的代码部分是:
<p></p> <pre><code>For Each OneElement In WebBrowser1.Document.GetElementsByTagName("input") If OneElement.GetAttribute("type") = "file" Then If OneElement.GetAttribute("name") = "file[]" Then 'Clicking the "Browse" button of the input type="file" OneElement.InvokeMember("click") 'MsgBox("Here#13") 'Trying to fill in the "FileName:" textbox of the popup form SendKeys.Send( _ "strig with the file address - e.g.: C:\Folder\File.html") 'Trying to click the open button, which is in focus SendKeys.Send(Chr(13)) End If Exit For End If Next </code></pre>
如上所述,
SendKeys.Send
仅在关闭文件上传弹窗后执行。编辑 #2:已解决! /14.04.2015 21:00/:
解决办法是这样创建一个新线程:
<p></p> <pre><code>... Dim tr As New System.Threading.Thread(AddressOf SendK) tr.Start() OneElement.InvokeMember("click") tr.Abort() ... Private Sub SendK() Threading.Thread.Sleep(2000) ' could be less SendKeys.SendWait("C:\TheFilePath.html") 'the file address path SendKeys.SendWait(Chr(13)) End Sub </code></pre>
对于任何想要升级它的人,因为它是一个原始解决方案:
1.可能是新线程可以关联按钮的onclick事件;
最佳答案
这里也是一样的:how to upload files with asp-classic
所以你想做的事情很简单。您可以像他们在 OneFineDay 提供的链接中那样设置您的 html 表单。
<form action="demo_form.asp">
<input type="file" name="pic" accept="image/*">
<input type="submit">
</form>
然后设置脚本。例如:
Dim objUpload
Dim strFile, strPath
' Instantiate Upload Class '
Set objUpload = New clsUpload
strFile = objUpload.Fields("file").FileName
strPath = server.mappath("/data") & "/" & strFile
' Save the binary data to the file system '
objUpload("file").SaveAs strPath
Set objUpload = Nothing
只需稍微尝试一下,您就会掌握它的窍门。
关于html - VB.Net:如何上传 <input type ="file"... > 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29501781/