Jump to content
Sign in to follow this  
autoituser123

winHTTP Upload Binary Data

Recommended Posts

autoituser123

Hello Guru

I'm trying to upload binary Data to a sample test web site.

I do FileRead($file_to_upload,16) and post uploading using WinHTTP routine.

The file is uploaded but corrupted. The fileRead function skips Null character and makes the file corrupted.

Any idea?

Thank you so much.

Share this post


Link to post
Share on other sites
BrettF

Welcome to the forum.

My crystal ball is cloudy.

Please post your code or a reproducer of your problem so we may assist you. We cannot help you without that.

Cheers

Share this post


Link to post
Share on other sites
IchBistTod

are you making sure to url encode the data before transmitting it?

if so you may want to try converting it to hex before uploading so that everything in the data is then represented by a number,then using php to turn it back into binary on the server side.

BTW, fileread is not skipping anything thats simply not possible, the issue is in your handling of the data.

hope this helps


[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites
suthers

Code sample definately required. A flag of 16 for binary usage is used with FileOpen(), not FileRead(). Using it with FileRead() will only read the first 16 characters.

Share this post


Link to post
Share on other sites
autoituser123

Hello

Thanks for all replies.

@IchBistTod, I'm not clear about how to convert it to hex before uploading so that everything in the data is then represented by a number,then using php to turn it back into binary on the server side. When I use FileRead, it's something like 0x332330x343333 ... For PHP, I find only number conversion functions like bin2hex, hexdec not data format conversion.

Here's my code:

#include "WinHTTP.au3"

#include <IE.au3> 


$LocalIP = "localhost"
$PostURL = "/upload_php.php"
$hw_open = _WinHttpOpen()

$hw_connect = _WinHttpConnect($hw_open, $LocalIP)

$h_openRequest = _WinHttpOpenRequest($hw_connect, "POST", $PostURL)
$upfile = "test.jpg";
$upsize = FileGetSize($upfile)

$file = FileOpen($upfile , 16)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
$line = ""

While 1
    $line &= FileReadLine($file) 
    If @error = -1 Then ExitLoop
Wend

FileClose($file)

$upcontent = $line

$data = "-----------------------------41184676334" & @CRLF &  'Content-Disposition: form-data; name="userfile"; filename="' & $upfile & '"' & @CRLF &   'Content-Type: application/octet-stream' & @CRLF & @CRLF &  $upcontent & @CRLF & '-----------------------------41184676334--' & @CRLF 

_WinHttpAddRequestHeaders($h_openRequest,"Content-Type: multipart/form-data; boundary=---------------------------41184676334")

_WinHttpSendRequest($h_openRequest,$WINHTTP_NO_ADDITIONAL_HEADERS, $WINHTTP_NO_REQUEST_DATA, StringLen($data), 0)

_WinHttpWriteData($h_openRequest, $data)

_WinHttpReceiveResponse($h_openRequest)

$oIE=_IECreate ()
_IEDocWriteHTML ( $oIE, _WinHttpReadData($h_openRequest))


_WinHttpCloseHandle($h_openRequest)

_WinHttpCloseHandle($hw_connect)

_WinHttpCloseHandle($hw_open)
Edited by autoituser123

Share this post


Link to post
Share on other sites
autoituser123

Does anyone care to find solutions for my code?

:)

Share this post


Link to post
Share on other sites
ProgAndy

Try this: http://www.autoitscript.com/forum/index.php?showtopic=84133&view=findpost&p=610967


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
ProgAndy

Oops, replace

Local $szExt = StringLower(StringRegExpReplace(,".*(?:\.([^.\\/]*))?\Z","$1"))

with

Local $szExt = StringLower(StringRegExpReplace( $path ,".*(?:\.([^.\\/]*))?\Z","$1"))


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.