flashlab Posted February 23, 2011 Share Posted February 23, 2011 (edited) I wanna to download a jpg file via winhttp. First I receive the binary data as string. Just like '0xFFD8FFE000104A...'. Then write it to file like↓ expandcollapse popup#include <WinHttp.au3> Local $jpg = _load('img3.douban.com','/lpic/s3292023.jpg',2) $jptg = StringMid($jpg,1,16386) $temp_JPG=FileOpen(@ScriptDir&'\img\s3292023.jpg',26) ;~ MsgBox(0,BinaryLen(Binary($jptg))&' '&BinaryMid(Binary($jptg),1,2),IsBinary(Binary($jptg))) FileWrite($temp_JPG,Binary($jptg)) FileClose($temp_JPG) $jptg2 = StringMid($jpg,1,16388) $temp_JPG=FileOpen(@ScriptDir&'\img\s3292024.jpg',26) ;~ MsgBox(0,BinaryLen(Binary($jptg2))&' '&BinaryMid(Binary($jptg2),1,2),IsBinary(Binary($jptg2))) FileWrite($temp_JPG,Binary($jptg2)) FileClose($temp_JPG) Func _load($hos,$req, $fo, $agent='',$fl='GET',$adata='') Local $hOpen, $hConnect, $hRequest, $sReturned If $agent='' Then $hOpen = _WinHttpOpen() Else $hOpen = _WinHttpOpen($agent) EndIf $hConnect = _WinHttpConnect($hOpen, $hos) $hRequest = _WinHttpOpenRequest($hConnect, $fl, $req) _WinHttpSendRequest($hRequest,'',$adata) _WinHttpReceiveResponse($hRequest) If _WinHttpQueryDataAvailable($hRequest) Then ; if there is data Do $sReturned &= _WinHttpReadData($hRequest,$fo) Until @error EndIf _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) Return $sReturned EndFunc the strange thing is ,when I pick up 8192 bytes, it can't normal save, else it will save as string... Is Binary() has this number limitation? Edited February 24, 2011 by flashlab Link to comment Share on other sites More sharing options...
jchd Posted February 23, 2011 Share Posted February 23, 2011 (edited) You seem to be mixing several points here.What does _httpload() return?StringMid will always return a *string*BinaryMid will work on a binary variant and return a Binary variant.bits are not bytes.O yes, BTW, as explained in the help file, Language Reference > Datatypes § Binary you have that "Binary data, can contain up to 2147483647 bytes"That should make any .JPG fit in a binary variant. Edited February 23, 2011 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
flashlab Posted February 24, 2011 Author Share Posted February 24, 2011 You seem to be mixing several points here. What does _httpload() return? StringMid will always return a *string* BinaryMid will work on a binary variant and return a Binary variant. bits are not bytes. O yes, BTW, as explained in the help file, Language Reference > Datatypes § Binary you have that "Binary data, can contain up to 2147483647 bytes" That should make any .JPG fit in a binary variant. Sorry for my poor English I've update the code. It now can be debuged directly. you will understand what I mean~ expandcollapse popup#include <WinHttp.au3> Local $jpg = _load('img3.douban.com','/lpic/s3292023.jpg',2) $jptg = StringMid($jpg,1,16386) $temp_JPG=FileOpen(@ScriptDir&'\img\s3292023.jpg',26) ;~ MsgBox(0,BinaryLen(Binary($jptg))&' '&BinaryMid(Binary($jptg),1,2),IsBinary(Binary($jptg))) FileWrite($temp_JPG,Binary($jptg)) FileClose($temp_JPG) $jptg2 = StringMid($jpg,1,16388) $temp_JPG=FileOpen(@ScriptDir&'\img\s3292024.jpg',26) ;~ MsgBox(0,BinaryLen(Binary($jptg2))&' '&BinaryMid(Binary($jptg2),1,2),IsBinary(Binary($jptg2))) FileWrite($temp_JPG,Binary($jptg2)) FileClose($temp_JPG) Func _load($hos,$req, $fo, $agent='',$fl='GET',$adata='') Local $hOpen, $hConnect, $hRequest, $sReturned If $agent='' Then $hOpen = _WinHttpOpen() Else $hOpen = _WinHttpOpen($agent) EndIf $hConnect = _WinHttpConnect($hOpen, $hos) $hRequest = _WinHttpOpenRequest($hConnect, $fl, $req) _WinHttpSendRequest($hRequest,'',$adata) _WinHttpReceiveResponse($hRequest) If _WinHttpQueryDataAvailable($hRequest) Then ; if there is data Do $sReturned &= _WinHttpReadData($hRequest,$fo) Until @error EndIf _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) Return $sReturned EndFunc Link to comment Share on other sites More sharing options...
PsaltyDS Posted February 24, 2011 Share Posted February 24, 2011 To repeat...StringMid will always return a *string*BinaryMid will work on a binary variant and return a Binary variant. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
ProgAndy Posted February 24, 2011 Share Posted February 24, 2011 I don't know, why you need StringMid or BinaryMid. You should just use _WinHTTPSimpleRequest with $iMode set to 2 (binary) #include<WinHTTP.au3> $hSession = _WinHTTPOpen() $hConnect = _WinHTTPConnect($hSession, "img3.douban.com") $bImage = _WinHTTPSimpleRequest($hConnect, "GET", "/lpic/s3292023.jpg", Default, Default, Default, Default, 2) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hSession) ; now $bImage contains the downloaded binary data of the image *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 Link to comment Share on other sites More sharing options...
jchd Posted February 24, 2011 Share Posted February 24, 2011 The mere idea of cutting a jpeg image in half or so is itself questionable! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
ProgAndy Posted February 24, 2011 Share Posted February 24, 2011 @jhcd: I don't think he wants to do that. @flashlab: Your problem is the function _load. It does not work when using binary as return type. *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 Link to comment Share on other sites More sharing options...
flashlab Posted March 1, 2011 Author Share Posted March 1, 2011 (edited) @jhcd: I don't think he wants to do that. @flashlab: Your problem is the function _load. It does not work when using binary as return type. OK Let's see something intuitive testwrite.au3 If nothing was wrong, a normal zip file will be created,whose size is half of the scriptfile. But I get twice the size than as expect. If i cut the binary into less than 8000b, I will get a part of zip file.SO, , , ----------------------------------------------------- Why don't you just use the code in post #5 to create your _load-function? Your problem is a really simple one: - You create an empty variable. - you append binary data to this variable. If appending to an empty variable, STRING-type is assumed and the binary is converted to string - Now you have the string 0x0123456789 in your variable. - Append new binary data 0xABCDEF. This is converted to string, too. 0x01234567890xABCDEF. The length of 8000 is the default length, one call to ReadData fetches. If you now cut the strng down to this length, you get a string, that consists only of 0x at the beginning and then only characters 0-9, A-F. This can be converted to binary again. A string like 0x12340xABCD cannot. To fix this problem, you have to initalise $sReturned as Binary('') when $fo is binary. But it is really easier to use the predefined functions where this is all handled automatically Thanks! I've got the point! Edited March 1, 2011 by flashlab Link to comment Share on other sites More sharing options...
ProgAndy Posted March 1, 2011 Share Posted March 1, 2011 Why don't you just use the code in post #5 to create your _load-function? Your problem is a really simple one: - You create an empty variable. - you append binary data to this variable. If appending to an empty variable, STRING-type is assumed and the binary is converted to string - Now you have the string 0x0123456789 in your variable. - Append new binary data 0xABCDEF. This is converted to string, too. 0x01234567890xABCDEF. The length of 8000 is the default length, one call to ReadData fetches. If you now cut the strng down to this length, you get a string, that consists only of 0x at the beginning and then only characters 0-9, A-F. This can be converted to binary again. A string like 0x12340xABCD cannot. To fix this problem, you have to initalise $sReturned as Binary('') when $fo is binary. But it is really easier to use the predefined functions where this is all handled automatically *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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now