News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Download a fat file from the Internet

Started by jj2007, September 05, 2023, 11:42:49 AM

Previous topic - Next topic

jj2007

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

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


NoCforMe

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.
Assembly language programming should be fun. That's why I do it.

jj2007

Dear David,

Sorry, you are here in the MasmBasic & the RichMasm IDE section. All sources posted here use a library. 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 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 :cool:

NoCforMe

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.)
Assembly language programming should be fun. That's why I do it.

jj2007


NoCforMe

Assembly language programming should be fun. That's why I do it.