tags:

views:

480

answers:

3

Hello,

I have a WinForms C# application. There is a WebBrowser control on the form named "browser".

Also I have following code:

            HtmlDocument doc = browser.Document;

            HtmlElement mForm = doc.GetElementById("TheFormId");

            doc.GetElementById("Name").SetAttribute( "value", "Some Name" );

            HtmlElement elFile = doc.GetElementById( "TheFile" );
            elFile.Focus();
            SendKeys.Send( "C:\\1.txt" );

            mForm.InvokeMember( "submit" );

The problem is that it does not submit a file. If I manually type in file name in the corresponding input box - it works.

Environment: Win XP SP2, IE6, VS 2008

Edit: This fixed the problem:

SendKeys.Send( "C:\\1.txt" + "{ENTER}" );
A: 

Does your <form> element have the enctype="multipart/form-data" attribute? You need this to upload files in a form.

Matt Wrock
Hello Matt, thanks for the answer. Yes, this form have enctype="multipart/form-data"And also I can submit file manually.
Oleg
+1  A: 

I think this might help:

http://stackoverflow.com/questions/342453/c-net-uploading-file-to-a-web-form-using-httpwebrequest

Lukas Šalkauskas
Thanks for the tip but how can I connect WebClient and WebBrowser? Please note that there are 20+ other fields in this form and I should submit them all + file. And show submission result to user in a WebBrowser control.
Oleg
if you use firefox, install add-on called Live HTTP headers (http://livehttpheaders.mozdev.org/). Then fill that form by hands, start this add-on, submit your form, and you will see all headers which was sent. Now you can do the same in the code. If you need more help, please let me know, I have some examples in C#.
Lukas Šalkauskas
A: 

This fixed the problem:

SendKeys.Send( "C:\\1.txt" + "{ENTER}" );
Oleg