Jump to content

Upload Image WinHttp


Go to solution Solved by MadaraUchiha,

Recommended Posts

Posted

Hm, sorry for that, but I am really overwhelemed with this...

I tried everything...

And always teh file is corrupted...

I tried FileOpen with param 0 and param 16, still corrupted.

Also tried Binary and BinaryToString.

I can't understand why the file is corrupted and it won't just upload a valid file? :/

  • Solution
Posted

Finally solved it!

Just for everyone else, if someone will ever need this:

If you like to upload a file to your webserver, without using FTP, then just use this AutoIt script:

$File = @ScriptDir & "\test.jpg"
$sHost = "www.yourserver.com" 
$sFormAction = "/postdata.php"

$hfile = FileOpen($File, 16)
$sFileTypeName = StringRegExpReplace($File, '^.*\\', '')

While 1
    $data = FileRead($hfile, 500000) ;500000 = 1??
    If @error Then ExitLoop
    Global $Data2 = StringTrimLeft($data,2)
    SendPost()
WEnd

Func SendPost()
$oRequest = ObjCreate('WinHttp.WinHttpRequest.5.1')
$oRequest.Open('POST', 'http://' & $sHost & $sFormAction, 0)
$oRequest.SetRequestHeader('User-Agent', 'Mozilla/4.0 (Windows XP 5.1)')
$oRequest.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oRequest.SetRequestHeader('Host', $sHost)
$oRequest.Send('filename=' & $sFileTypeName & '&data=' & $Data2)
$sData = $oRequest.ResponseText
MsgBox(0, 'Data', $sData)
EndFunc

And your PHP Script should look like this:

<?php
$fileName = $_POST['filename'];
$binaryData = $_POST['data'];

$fh = fopen("./$fileName", 'a+b');
fwrite($fh, pack("H*" , $binaryData));
fclose($fh);
?>

Posted

 

Finally solved it!

Just for everyone else, if someone will ever need this:

If you like to upload a file to your webserver, without using FTP, then just use this AutoIt script:

$File = @ScriptDir & "\test.jpg"
$sHost = "www.yourserver.com" 
$sFormAction = "/postdata.php"

$hfile = FileOpen($File, 16)
$sFileTypeName = StringRegExpReplace($File, '^.*\\', '')

While 1
    $data = FileRead($hfile, 500000) ;500000 = 1??
    If @error Then ExitLoop
    Global $Data2 = StringTrimLeft($data,2)
    SendPost()
WEnd

Func SendPost()
$oRequest = ObjCreate('WinHttp.WinHttpRequest.5.1')
$oRequest.Open('POST', 'http://' & $sHost & $sFormAction, 0)
$oRequest.SetRequestHeader('User-Agent', 'Mozilla/4.0 (Windows XP 5.1)')
$oRequest.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
$oRequest.SetRequestHeader('Host', $sHost)
$oRequest.Send('filename=' & $sFileTypeName & '&data=' & $Data2)
$sData = $oRequest.ResponseText
MsgBox(0, 'Data', $sData)
EndFunc

And your PHP Script should look like this:

<?php
$fileName = $_POST['filename'];
$binaryData = $_POST['data'];

$fh = fopen("./$fileName", 'a+b');
fwrite($fh, pack("H*" , $binaryData));
fclose($fh);
?>

Glad you found a solution. That is autoit scripting but not pure autoit. Your script will just crash in case your winhttp object returns an error.

But since this thread is marked as solved then there is not much more to say ;)

  • 9 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...