2

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)

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
modzsi
  • 185
  • 2
  • 12
  • 5
    `not working` is not a very good forensic term. What is happening in browser console when look at errors, check request in network tab etc. – charlietfl Nov 03 '13 at 22:13
  • not working = 'filename' field did not get updated... not sure what browser console i supposed to check website ONLY works in IE. I am using TWebBrowser component. – modzsi Nov 03 '13 at 23:04
  • ie has a developers tools console- F12 on keyboard – charlietfl Nov 03 '13 at 23:05
  • Thanks for that, but not sure how could this help me. Also I could not locate the 'Network Tab'. I am trying to automate the form from an app written in Lazarus, I am using Fiddler to see what is going on. – modzsi Nov 03 '13 at 23:49
  • 1
    These are two totally different questions (one about HTML/JavaScript, and a totally separate one about Lazarus/Synapse). They need to be posted separately, with the appropriate tags for each question. This site is designed around "one question, one answer", not "lump a bunch of non-related questions together and ask for them all to be answered". :) Please [edit] to remove one question and its related tags, and ask that removed question in a new post. Thanks. – Ken White Nov 04 '13 at 00:15
  • You're asking how to pre-populate a file-input field on an HTML form. That's not allowed. It would be a security risk — a Web page could pre-populate the field with, say, *secret-passwords.db*, and then trick the user into submitting the form, thus collecting files that users don't really want to share. You'll need to think of some other way of solving your problem. – Rob Kennedy Nov 04 '13 at 01:33
  • This is a single question with pretty much relevant tags. Maybe javascript is not needed tough... The question is in the title, and the rest is just the details... I would like to achieve what is in the there. I would seperate the two if they were unrelated. But they are not... – modzsi Nov 04 '13 at 01:34
  • Rob: Thanks for your answer. I would like to automate a file upload to a website where I am logged in. My app logs in goes to the page where this file input is located (among other inputs). I am just thinking that it should be possible to bypass Browsing the file and just upload the file. I am not sure how as when I try it just goes to the logoff page of the site. – modzsi Nov 04 '13 at 01:41
  • http is not the right protocol in this case, i would say. Fiddling with input-fields and actions in html sounds like a security-problem, as Rob said. As the name suggests, FTP (File Transfer Protocol) would be a much better choice. – Andreas Nov 04 '13 at 07:03

1 Answers1

0

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.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486