How to set value of <INPUT type=file...> element programmatically?

E

Evgeny Zoldin

Hi ALL.

I need to simulate send POST data ( - file upload) as it was submitted in Browser. In C# I create instanse of IE, navigate to page with the form, fill in the fields and try to Submit by call submitButton.click() Unfortunately it does not work while file_filed.vale = <path_to_file> has no effect, value <INPUT type=file...> cound not be set from script of that HTML page. Is there another way to set that value by means of ..NET ? Below the part of code :

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();
object o = null;
ie.Navigate( url, ref o, ref o, ref o, ref o ); // navigate to page with the form and waiting for DownloadComplete


..............................................


mshtml.HTMLDocument doc = (mshtml.HTMLDocument)ie.Document;
mshtml.IHTMLInputFileElement file = (mshtml.IHTMLInputFileElement)doc.getElementsByName("file").item(null,0);
file.value = m_sPathToFile; // has no effect!!!!!!!
mshtml.HTMLInputButtonElement btn = (mshtml.HTMLInputButtonElement)doc.getElementsByName("send").item(null,0);
btn.click();

.........................



Thank you for any help

Evgeny
 
I

Igor Tandetnik

Evgeny Zoldin said:
I need to simulate send POST data ( - file upload) as it was
submitted in Browser. In C# I create instanse of IE, navigate to page
with the form, fill in the fields and try to Submit by call
submitButton.click() Unfortunately it does not work while
file_filed.vale = <path_to_file> has no effect, value <INPUT
type=file...> cound not be set from script of that HTML page. Is
there another way to set that value by means of .NET ?

There is no way to set the value programmatically, with .NET or
otherwise, for security reasons. value property on <input type="file">
element is read-only.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
 
S

Shiva

Hi,
It can't be done programmatically; user has to manually select the file for
uploading.

Hi ALL.

I need to simulate send POST data ( - file upload) as it was submitted in
Browser. In C# I create instanse of IE, navigate to page with the form, fill
in the fields and try to Submit by call submitButton.click() Unfortunately
it does not work while file_filed.vale = <path_to_file> has no effect, value
<INPUT type=file...> cound not be set from script of that HTML page. Is
there another way to set that value by means of .NET ? Below the part of
code :

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorerClass();
object o = null;
ie.Navigate( url, ref o, ref o, ref o, ref o ); // navigate to page with the
form and waiting for DownloadComplete

..............................................

mshtml.HTMLDocument doc = (mshtml.HTMLDocument)ie.Document;
mshtml.IHTMLInputFileElement file =
(mshtml.IHTMLInputFileElement)doc.getElementsByName("file").item(null,0);
file.value = m_sPathToFile; // has no effect!!!!!!!
mshtml.HTMLInputButtonElement btn =
(mshtml.HTMLInputButtonElement)doc.getElementsByName("send").item(null,0);
btn.click();
.........................

Thank you for any help
Evgeny
 
Joined
Apr 2, 2011
Messages
3
Reaction score
0
In Excel I do it this way:

Simply load form, have the focus change to the File input control, and then send keys command to type in the file name and submit.

Sub UploadFile()

IE.Navigate "http://some_twiki_page.com/upload.html"

' Statusbar
Application.StatusBar = "Twiki Page is loading. Please wait..."

' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop

' Find 2 input tags:
' 1. Text field
' <input type="text" class="textfield" name="s" size="24" value="" />
'
' 2. Button
' <input type="submit" class="button" value="" />

Application.StatusBar = "Search form submission. Please wait..."

Set objCollection = IE.document.getElementsByTagName("input")

i = 0
While i < objCollection.Length

If objCollection(i).Name = "filepath" Then
objCollection(i).Focus
SendKeys (sFilePath)
Else

If objCollection(i).Type = "submit" And _
objCollection(i).Value = "Upload file" Then

' "Search" button is found
Set objElement = objCollection(i)

End If
End If
i = i + 1
Wend


objElement.Click ' click button to upload

end sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top