Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

webbrowser control - Upload a file to a website programmatically?

I am using Lazarus I have an app with webbrowser component on it that logs into a website loads a page as below (see html code below), and fills in different inputs. The last input is a file to upload. I would like my app to "click" Browse, select a file i want to, and Open. After that I could do a post the form OR just upload the file and carry on.

1

I have the following html code on the site:

<td align="left" class="RequiredInput">File:</td>
<td class="datafield">
 <form name="frmMain" id="frmMain" action="upload.asp?step=2&output=1" method="post" enctype="multipart/form-data">
    <input type="file" name="filename" id="filename">
</form>

I tried executing JS from my app : document.getElementById('filename').value = 'C:x.csv'

enter image description here

2

I tried using the following code HttpPostFile from synapse:

uFileName := 'C:x.csv';
uStream := TFileStream.Create(uFileName, fmOpenRead);
uList:=TStringList.Create;  
if HttpPostFile('upload.asp?step=2&output=1', 'filename', uFileName, uStream, uList) then
 ShowMessage('OK');   

It did nothing at all (I followed the activity of the app with Fiddler)

Any help would be appreciated.

Thanks in advance,

Zsolt

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a known problem, and there is a solution, but you're going to have to convert it from С# to Delphi.

Another possible solution is to upload the file using URL Moniker APIs. The upload will then happen on the same session the WebBrowser control is already using. There is an MSKB article:

How To Handle POST Requests in a Pluggable Protocol Handler

The POSTMON.EXE sample linked to the article has disappeared from Microsoft website, but still can be found here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...