Upload File from WebCore to XData Service

Is there an example of using TWebFilePicker and then uploading the file to an XData Service?

Not sure if there is an example anywhere, but what parts are you interested in?

I kinda do something like this, uploading a file to the XData server. The gist of it is like this.

WebCore:

  1. Create a WideString variable containing the Base64-encoded data.
  2. eg: SendFile := Window.BtoA(SomeFile); where SendFile is a WideString.
  3. Not clear how you get the SomeFile in there, but probably not too hard.
  4. Make usual call Response := await(Client.RawInvokeAsync(Service,[SendFile]));

XData:

  1. Service should be setup with [HttpPost] so that SendFile can be arbitrarily large.
  2. Use TNetEncoding.Base64.Decode from SysUtils.NetEncoding unit to decode to stream.
  3. In my case, I just dump the file directly into a database still base-64 encoded.
2 Likes

Thanks Andrew. I was looking at your example that worked the other way and was thinking of reversing it.

Pretty much. The only thing that tripped me up was dealing with larger files. The limit for [HttpGet] is something unusual like 8k or something weird. If you're dealing with databases, they may have different limits as well. So took me awhile to figure out where the limitation was coming from. But now I'm good apparently up to 4GB :thinking:

In my case I'm using it as a sort of storage facility for HTML where the HTML can contain embedded images. So it was working fine initially with just simple HTML examples but when I put a real image into it, it failed rather unexpectedly. All good now though. I should probably store the HTML in the database unencoded so it can be searched, now that I think about it. But for arbitrary files where they could be binary or whatever, keeping them in base64-encoded while shuffling them around makes it a lot easier to keep track of things as well as test the XData side.

Looks like the TWebFileUpload does varying conversions - to Base64, to Stream. so I'll have a play with that tomorrow, getting late now

Hope it all worked out for you? I had a bit of a glitch today with the Base64 stuff.

I was trying to encode and upload a bit of content from an HTML editor (SunEditor) and it creates data I guess that is not just ascii characters when embedding images. Or at least it does sometimes because it was working before. But today, Window.btoa failed with an error about not being able to encode characters outside of the Latin1 range. So I added js-base 64:

<script src="https://cdn.jsdelivr.net/npm/js-base64@latest/base64.js"></script>

And then use

asm
  SendFile = Base64.encode( sourcefile );
end;

and it works as it did previously.

1 Like

That's worth knowing. At teh moment I am just uploading csv files, so I haven't come across that issue yet. I am using the TWebFilePicker and the TWebFilePicker.Files[0].GetFileAsBase64 which works well.