0

I have a webpage with a file upload dialog. I'm viewing this page from my WebBrowser control in my Windows Forms application. I'd like to automatically enter and submit the form.

I realise this question has been asked before (a lot). I've seen so many of them I lost the count. But none of the solutions have worked for me.

My Webpage looks like

<form method="post" enctype="multipart/form-data">
   <input id="file" name="file" type="file"/>
   <input type="submit"/>
</form

My C# application looks like

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webBrowser1.Document.GetElementById("file").Focus();
        SendKeys.Send("C:\\Users\\Merlin Sweden\\Desktop\\extraDirTest\\myImage.jpg{ENTER}");
    }
}

From all the things I've tried this at least felt like I was getting close. Anyway, when executing the dialog opens and part of the path is entered (weden\Desktop\extraDirTest\myImage.jpg). I thought it might be some sort of maximum input length, I've added another directory in the path (extraDirTest) and discovered that that was not the issue.

So I'm thinking it is related to the space BUT i cannot find any alternative to enter a space. Also if the PATH i send is incorrect the dialog doesn't open at all. And if i place a textbox on my form and try to enter it in there then all works fine.

I don't understand what is happening or how I should fix this.

Can anyone help me?

Thank you

Edit Perhaps it is worth mentioning that moving the file to "C:\myImage.jpg" and changing the path doesn't do anything.

user6
  • 1,999
  • 2
  • 23
  • 29
  • possible duplicate of [How do you programmatically fill in a form and 'POST' a web page?](http://stackoverflow.com/questions/26857/how-do-you-programmatically-fill-in-a-form-and-post-a-web-page) – Peter Ritchie Apr 02 '13 at 13:59
  • sounds like you will need to use `JavaScript` to perform the `button_click` have you tried doing that or googling on how to `Invoke Button_Click()` using `JavaScript`? – MethodMan Apr 02 '13 at 14:00
  • @PeterRitchie nope, I'm not using ASP.NET. But there are plenty of duplicate questions around there. – user6 Apr 02 '13 at 14:04
  • @user6 you can use WebRequest for WinForms too... What you're asking to do isn't specific to WinForms applications. – Peter Ritchie Apr 02 '13 at 14:05
  • @DJKRAZE That would open the upload dialog which I have already achieved. Getting the correct value entered within the dialog is the problem I'm facing. – user6 Apr 02 '13 at 14:06
  • @PeterRitchie I realise I could do a WebRequest without the webBrowser. This is however due to circumstances undesired. I would very much like to get it working through the webBrowser control with the file input field. – user6 Apr 02 '13 at 14:09
  • which one have you tried the first example , second one or both.. I thought on the `__doPostBacm` is where you could do what ever it is you wanted to do prior to the actual form doing a full `PostBack` perhaps I have misunderstood your initial question.. I will remove my answer.. – MethodMan Apr 02 '13 at 14:13
  • You know there is another trick to getting the correct value I need to remember the syntax let me try to find my code I've done this before – MethodMan Apr 02 '13 at 14:18
  • @user6 Why? You know the particular action you want to perform results in a web request. Seems much easier to simply make the request rather than using the browser control in uncommon ways. – Peter Ritchie Apr 02 '13 at 14:20
  • It looks like it might be a timing issue. Have you considered a Thread.Sleep of about 100ms before using sendkeys? Might take a bit of tweaking to get it working right - allow more time than you think is necessary to ensure it works on slower PCs than yours. – sga101 Apr 02 '13 at 14:29
  • couldn't you do something like this to get the value `string selectedValue = Request.Form[yourcontrol.UniqueID];` – MethodMan Apr 02 '13 at 14:40

1 Answers1

0

The reason only half of it is entered is because the 'focus' does not trigger the dialog. The space does. So the dialog is only opened after the space and whatever is sent after that it entered.

For some reason the character right after the space is also left out, probably related to timing.

I've ended up doing

SendKeys.Send("  C:\\extraDirTest\\sednasoftwaread.jpg{ENTER}");

Also afterwards you cannot instantly submit the form because then the form will be submitted before the input is filled. Sleeping also isn't the answer because the dialog sleeps along. Using a timer the form can be submitted after the input has been entered.

Everyone thank you for the help!

user6
  • 1,999
  • 2
  • 23
  • 29