I'm not sure if I covered this problem in the past. Normally, to download a file, you can do the following:
include \masm32\MasmBasic\MasmBasic.inc
Init
Let esi=FileRead$("http://www.jj2007.eu/TextForSorting.zip")
FileWrite "local.zip", esi, LastFileSize
EndOfCode
Works like a charm, but of course, this 81MB file fits in memory, so that was easy. What if the download was much, much bigger (as asked for in this thread (https://masm32.com/board/index.php?topic=11208.0))?
Then we have to read in chunks, asynchronously to avoid a hanging application, so it gets a bit more complicated:
GuiParas equ "Downloading 81MB", w200, h200, b LiteBlueGreen, icon Globe ; width+height, margins, background colour
include \masm32\MasmBasic\Res\MbGui.asm
GuiControl MyEdit, "edit", "Downloading... (hold Shift to cancel)"
Open "O", #1, "TextForSorting.zip"
Download "http://www.jj2007.eu/TextForSorting.zip", cb:MyCallback
Event Download
Close #1
AddWin$ hMyEdit=cfm$("\n*** download done ***")
EndOfEvents
MyCallback:
push edi
push eax ; #bytes read with this chunk
sub edi, eax ; at entry, edi=current buffer start + #bytes
Print #1:eax, edi ; print #eax bytes to file
pop ecx
AddWin$ hMyEdit=Str$("\n%i bytes downloaded at ", ecx)+fTime$() ; currrent state in edx
Delay 1
invoke GetKeyState, VK_SHIFT
test ah, ah ; cancel download if Sign? flag set
pop edi
retn
GuiEnd
The result (full project attached, building requires a not-so-old version of MasmBasic (http://masm32.com/board/index.php?topic=94.0)):
(http://www.jj2007.eu/pics/DownloadToBigLocalFile.png)
That's nice, Jochen, but do you think you could give us a version that doesn't require MasmBasic? like just plain MASM? For those of us who, believe it or not, like to code in regular vanilla old MASM?
I have no idea what "Event Download" ... EndOfEvents" or "Download ..." mean or do. I am, however, familiar with ReadFile(), which I'm guessing is what's actually being used behind the scenes.
Thanks.
OK, I guess owing to where this is it's kosher to use your IDE here. However, I came here out of general interest in the topic ("Download a fat file ..."), so it would still be nice to know the non-MasmBasic translation of this.
Dear David,
Sorry, you are here in the MasmBasic & the RichMasm IDE section. All sources posted here use a library (http://masm32.com/board/index.php?topic=94.0). If somebody uses print "Hello World" in his code, do you come along and ask to get the same but without that fancy print macro and its underlying \Masm32\m32lib\masm32.lib ?
Special service for you:
StdOut proc lpszText:DWORD
LOCAL hOutPut :DWORD
LOCAL bWritten :DWORD
LOCAL sl :DWORD
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hOutPut, eax
invoke StrLen,lpszText
mov sl, eax
invoke WriteFile,hOutPut,lpszText,sl,ADDR bWritten,NULL
mov eax, bWritten
ret
StdOut endp
invoke StdOut, chr$("Hello World") : print "Hello World" without using the print macro
Attention, you still need to lookup StrLen and chr$() yourself, but I spared you the print macro, knowing that you hate macros :cool:
Btw fearless (https://masm32.com/board/index.php?msg=123339) posted everything you need to roll your own. If you feel too tired to read his code, google UrlDownloadToFile. There is a wonderful, simple, example at SOF (//Should%20I%20use%20URLDownloadToFile?) :cool:
OK, that takes care of the writing side of things: how about the reading side? That's what I was most curious about here. (Related to that other thread here from colinr about reading internet data.)
Quote from: jj2007 on September 05, 2023, 06:42:10 PMBtw fearless (https://masm32.com/board/index.php?msg=123339) posted everything you need to roll your own.
Yes, nice!