[英]VB.Net: How to upload a file in a <input type=“file” … >
I am trying to make a Windows Forms Program to automatically upload a file to website. 我正在尝试制作Windows窗体程序以自动将文件上传到网站。
The website has a "Browse..." button from the input type="file">
tag, on which I InvokeMember("click")
that triggers the file picker, but I am unable to interact with it. 该网站有一个来自
input type="file">
标签的“Browse ...”按钮,我在其上触发文件选择器的InvokeMember("click")
,但我无法与之交互。
I am not familiar with that control. 我不熟悉那种控制。 I have tried with
SendKeys.Send("quotes.html") : SendKeys.Send(Chr(13))
, with no success at all. 我尝试使用
SendKeys.Send("quotes.html") : SendKeys.Send(Chr(13))
,但没有成功。 The SendKeys.Send
commands are executed only after I manually close the file picker. 只有在我手动关闭文件选择器后才会执行
SendKeys.Send
命令。
Does anyone have an idea how to choose a file in this <input type="file">
control using VB.NET? 有没有人知道如何使用VB.NET在这个
<input type="file">
控件中选择一个文件?
Edit #1 /08.04.2015 13:00/: 编辑#1 /08.04.2015 13:00 /:
The web service, where I upload the files is a 3rd party application, which I cannot modify. 我上传文件的Web服务是第三方应用程序,我无法修改。 I just want to interact with it so that my application can automatically upload the file, by interacting with the pop-up, file upload form, which appears, but I do not know how to do that.
我只是想与它进行交互,以便我的应用程序可以通过与弹出的文件上传表单进行交互来自动上传文件,但是我不知道该怎么做。
The part of my code for this procedure is: 我的代码部分是这个过程:
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
SendKeys.Send
is executed only after the file upload pop-up is closed.
SendKeys.Send
仅在文件上载弹出窗口关闭后执行。
Edit #2: SOLVED! 编辑#2:已解决! /14.04.2015 21:00/:
/14.04.2015 21:00 /:
The solution is to make a new thread like that: 解决方案是创建一个新的线程:
\n\n\n\n... 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
Its kind of the same over here: how to upload files with asp-classic 在这里它的类型相同: 如何使用asp-classic上传文件
So what you want to do is pretty easy. 所以你想做的事情很简单。 You set your html form like they did in the link OneFineDay provided.
您可以像在OneFineDay提供的链接中一样设置html表单。
<form action="demo_form.asp">
<input type="file" name="pic" accept="image/*">
<input type="submit">
</form>
and then you set up your script. 然后你设置你的脚本。 For example:
例如:
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
Just play around with it a little and you'll get the hang of it. 只需稍微玩一下就可以了解它。
The solution I was looking for is this: 我正在寻找的解决方案是这样的:
...
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
By adding the new thread I am finally able to interact with the input type="file">
form and I send a string with the address path of the file on my PC with the SendKeys.SendWait("path as string")
. 通过添加新线程,我终于能够与
input type="file">
表单进行交互,并使用SendKeys.SendWait("path as string")
在PC上发送带有文件地址路径的SendKeys.SendWait("path as string")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.