SRL Archive - OSR Help

File Uploading with web client functions


Shady?

2013-02-22 08:37:52

Hi guys!

I'm trying to get a file uploaded to a server using the POST and Client web functions in Simba.

Using APPA, I can do something like:

PostHTTPFormData('http://poster.decaptcher.com/',
['function', 'username', 'password', 'pict_to', 'pict_type'],
['picture2', decap_Username, decap_Password, '0', '0'],
['pict'], [ScriptPath+'Captcha.'+imgType], ['image/p'+imgType]);

Where I get to select the Content-Type: image/jpeg for the picture. But, I'm having troubles finding a way to do it just with Simba's webclients.
Would something like addpostvariable( c, 'pict', 'C:\a.JPG'); work? Is this even possible?

Thanks in advance!

Sex

2013-02-22 08:59:32

You'd probably have to send the HTTP request manually through sockets.

http://chxo.com/be2/20050724_93bf.html

Example:

Add additional mime type mappings as needed, and you can add response handling if you want.

Test by using http://kwollaston.com/shady/upload.php as URL and pick a path to a text file.
program Upload;

const
URL = 'http://example.com/upload.php';
FilePath = 'C:\test.jpg';

function ReadFile(Path : string) : string;
var
FileNum : integer;
begin
FileNum := OpenFile(Path, True);
ReadFileString(FileNum, result, FileSize(FileNum));
CloseFile(FileNum);
end;

procedure ParseURL(URL : string; out Host, Path : string);
var
o : integer;
begin
URL := Trim(URL);
if Pos('http://', URL) = 1 then
o := 8
else
o := 1;
Host := Copy(URL, o, PosEx('/', URL, o) - o);
Path := Copy(URL, PosEx('/', URL, o), Length(URL));
end;

function GetFilename(FilePath : string) : string;
var
i : integer;
begin
for i := length(FilePath) downto 1 do
if (FilePath[i] <> '/') and (FilePath[i] <> '\') then
result := FilePath[i] + result
else
break;
end;

function GetFileExtension(Filename : string) : string;
var
i : integer;
begin
if pos('.', Filename) = 0 then
exit;
for i := length(Filename) downto 1 do
if (Filename[i] <> '.') then
result := Filename[i] + result
else
break;
end;

function GetContentType(Extension : string) : string;
begin
case Extension of
'txt','simba' : result := 'text/plain';
'png' : result := 'image/png';
'jpg','jpeg' : result := 'image/jpeg';
'bmp' : result := 'image/bmp';
end;
end;

procedure Upload(URL, FilePath : string);
var
Client : integer;
Header, Data, Host, Path, Request : string;
begin
ParseURL(URL, Host, Path);
Header := 'POST ' + Path + ' HTTP/1.1' + #13#10 +
'Host: ' + Host + #13#10 +
'Content-Type: multipart/form-data; boundary=SimbaAaB03x' + #13#10;

Data := '--SimbaAaB03x' + #13#10 +
'Content-Disposition: form-data; name="userfile"; filename="' + GetFilename(FilePath) + '"' + #13#10 +
'Content-Type: ' + GetContentType(FilePath) + #13#10 + #13#10 +
ReadFile(FilePath) + #13#10 +
'--SimbaAaB03x--' + #13#10 + #13#10;

Header := Header + 'Content-Length: ' + ToStr(Length(Data)) + #13#10#13#10;
Request := Header + Data;
Client := CreateSocket();
ConnectSocket(Client, Host, '80');
SendSocket(Client, Request);
CloseSocket(Client);
FreeSocket(Client);
end;

begin
Upload(URL, FilePath);
end.

Shady?

2013-02-22 15:44:48

Thank you very much Sex. I'll give it a try now, also I'll give those articles a good read.

Will let you know how it works out! Thanks again, you rock.

Sex

2013-02-22 15:53:18

From: Shady?
Thank you very much Sex. I'll give it a try now, also I'll give those articles a good read.

Will let you know how it works out! Thanks again, you rock.


No problem, and yes, sex does rock! ;)