Author Topic: DownloadToStr$ function  (Read 387 times)

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 786
    • View Profile
DownloadToStr$ function
« on: July 01, 2020, 03:44:43 PM »
The built-in BCX command, DOWNLOAD (Url$, LocalFilename$)
makes it dead easy to download a file from a website to your local drive. 

But what if you don't want to save it as a file? 

Suppose instead you want to save it to a string inside your program. 

Now you have few steps:

1) Save the contents to a temporary file somewhere.
2) Determine the size of that file.
3) Dimension a string buffer large enough to hold those contents
4) Read the contents into that string buffer
5) Delete the temporary file.

That's what the function below does for you.  It is not intended for binary files but rather
 text based files like:  txt, csv, json, xml, html, and so on.

Compile the code below and run it.

It's not a complicated bit of code but I'll bet someone finds a use for it.

Code: [Select]

MSGBOX DownloadToStr$ ("https://www.bcxbasiccoders.com/msg.txt")


FUNCTION DownloadToStr$ (Url$)
   LOCAL Fname$
   Fname$=TEMPFILENAME$(TEMPDIR$, "any")
   
   DOWNLOAD (Url$,Fname$)
   
   DIM Buffer$ * LOF(Fname$) + 1
   Buffer$ = LOADFILE$(Fname$)
   
   KILL Fname$
   FUNCTION = Buffer$
END FUNCTION

« Last Edit: July 01, 2020, 07:39:49 PM by MrBcx »