Upload a file to a website programmatically?

别说谁变了你拦得住时间么 提交于 2019-11-28 05:59:43

问题


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'

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


回答1:


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.



来源:https://stackoverflow.com/questions/19758767/upload-a-file-to-a-website-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!