Downloading a file into a WEB Core app

My WEB Core client makes a request of my XData service, which then needs to return a series of MP3 files. What I'm getting back initially is a URL where each of the MP3 files live on a 3rd-party server. They get deleted "after about an hour", so I need to download them somewhere. (I actually need to use them within a few minutes, so having them deleted after an hour isn't an issue.)

I have three options:

  1. XData service simply returns the URL to the client and lets the client DL it;

  2. XData service DLs the file, saves it somewhere that can be accessed by the client and passes some kind of token or pathname / URL to it;

  3. Same as #2 but it also sends the file itself in the response.

These aren't really huge files, like maybe 10-20 seconds of audio.

Some of these files I want to cache for future use, and others I don't. So option 2 seems best.

My question is, if I'm going to DL the file from the WEB Core client, what's the best way to do it?

I've found some different ways and they seem to suggest that they trigger the browser's File Download feature, which I don't want -- this needs to be done silently. I've got dozens of these coming down to the client in response to requests to my service, and I don't want the user to have to keep getting alerted to each one; they need to be invisible. That's why I thought of option 3 -- have them sent as part of the XData responses.

Is there a specific WEB Core function I can call with a URL that will simply download a file and put it into a TMemoryStream that I can do what I want with it?

Or am I better off having the XData side send each one down to the client?

(The client queues them up into different tracks and they're streamed to the user as one larger composition.)

There is a demo in XData distribution that shows how to download/upload binary and text blobs. You can check it to have an idea.

It's in folder demos\web\blob.

1 Like

Thanks, I'll check it out. But would you mind mentioning which function call(s) to use for this use-case? There are several described in the manual and I can't really tell the difference between the ones that trigger a DL that the user sees via the browser and which ones are invisible to the user.

Practically everything the browser does starts with a "download". Most are transparent, like when getting images and JS files referenced in web page headers, but some are treated as user-initiated and are visible in different ways. What call(s) would be used for the former (invisible) types of DLs? (I wish the doc was a little more clear about when to use different calls.)

To be honest I don't know what you are asking, which "function calls"? Basically you invoke a XData endpoint and handle the binary response. The demo shows how to do it then you adapt to your scenario.

I'm talking about downloading a file from a 3rd-party server on the WEB Core side, not the XData side. It's a WEB Core question. I found a half-dozen different Download methods and I can't really tell the difference between them. Some are in WEB Core and some are in Javascript in the DOM. And from random comments I saw, some are equivalent to clicking the download button on a file in the browser -- which is not what I want. (If you have your browser set to always ask where to save the file, then that dialog pops up. Otherwise, you just get stuff that shows the progress of the DL. You don't see this when the browser is downloading assets linked to the web page as it's being pulled down.)

TBH, I find myself getting confused when working in WEB Core because I catch myself thinking from the perspective of Delphi working inside of the machine, not the browser. It can be a bit disorienting sometimes. :slight_smile:

The TApplication object has different Download() methods.
As I assume what you have is an URL, so the method to use is Application.Download(URL) and that will trigger a download of the file from this URL.
Other methods assume there is within the scope of your web client app either text or binary data that you want to save as a file and therefore, use DownloadTextFile(aText: string) or DownloadBinaryFile(data); Given what I assume from your description, the data is in the backend, these do not apply.

Thanks Bruno. The file originates on a 3rd-party server. My XData service needs to DL it, and some of them I'll cache. I don't know whether it's easier or better to have the client DL some files directly or have the service forward everything on.

I saw that Download(URL) method but where does it download to? That's the problem with most of these ... they seem to go to the host file system (although that's not totally clear), which I don't want. I want them in TMemoryStreams. Saving them to disk then having to load them right back into a TMemoryStream and delete them from disk seems like a wasted effort.

Is there one that takes a URL and lets me DL it into a TMemoryStream?

This performs a synchronous load from an URL into a memorystream:

ms: TMemoryStream;
ms := TMemoryStream.Create;
ms.LoadFromURL(URL, false);

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.