Jump to content

Upload Image WinHttp


Go to solution Solved by MadaraUchiha,

Recommended Posts

  • Developers

You are becoming a little impatient here in this thread.

Please read our forum rules and don't bump threads within 24 hours.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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? :/

Link to comment
Share on other sites

  • Developers

Maybe you have still have a PHP issue? Shouldn't you be opening the file in binary mode?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

You mean on PHP side?

Stop asking and start trying/researching.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Solution

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);
?>

Link to comment
Share on other sites

 

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 ;)

Link to comment
Share on other sites

  • 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
 Share

  • Recently Browsing   0 members

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