'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'
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)
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |