Oleg
Oleg

Reputation: 601

Submit file in form from C#

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}" );

Upvotes: 2

Views: 5298

Answers (3)

Oleg
Oleg

Reputation: 601

This fixed the problem:

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

Upvotes: 0

Lukas Šalkauskas
Lukas Šalkauskas

Reputation: 14361

I think this might help:

C# .NET Uploading file to a web form using HttpWebRequest

Upvotes: 1

Matt Wrock
Matt Wrock

Reputation: 6640

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

Upvotes: 0

Related Questions