3


I am trying to make a Windows Forms Program to automatically upload a file to website.
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.

I am not familiar with that control. I have tried with SendKeys.Send("quotes.html") : SendKeys.Send(Chr(13)) , with no success at all. The SendKeys.Send commands are executed only after I manually close the file picker.

Does anyone have an idea how to choose a file in this <input type="file"> control using VB.NET?

Edit #1 /08.04.2015 13:00/:

The web service, where I upload the files is a 3rd party application, which I cannot modify. 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


As mentiontioned above, the SendKeys.Send is executed only after the file upload pop-up is closed.

Edit #2: SOLVED! /14.04.2015 21:00/:

The solution is to make a new thread like that:



...
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


For whoever whants to upgrade it, as it a raw solution:
1. May be the new thread can be associated with the onclick event of the button;
Ivo
  • 143
  • 1
  • 9

2 Answers2

1

Its kind of the same over here: how to upload files with asp-classic

So what you want to do is pretty easy. You set your html form like they did in the link OneFineDay provided.

<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.

Community
  • 1
  • 1
Mart N
  • 133
  • 10
1

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").

Ivo
  • 143
  • 1
  • 9