« Linux Compiler Upgrad… | Home | Customize your FileMa… »

Looking into WebUploadedFile class


The Xojo Web framework contains a special class to hold information on an uploaded file, the WebUploadedFile class. Used by the WebFileUploader control this class provides you the information of the uploaded file. Let's take a deeper look:

An uploaded file can either be backed in memory or backed by a temporary file. There is a threshold when files are put in memory or into a file and we don't know when exactly this happens. But there may be multiple reasons like memory usage.

Consequentially if you query length, you are returned the length of the MemoryBlock or the temporary file. If you query Data, you get back the MemoryBlock or for a file backed one, the data is read and returned. This means the file has to fit in memory to not cause an exception. And it may cause a delay for the time to load the file. Sadly the trigger to loading data in memory may be when you stop in debugger and inspect the WebUploadedFile file. You then see file property and data property have content.

The UploadComplete event is where you get the array of the uploaded files. Here you may process the data, e.g. take it pass it to Picture.FromData to read in a picture. If you store the WebUploadedFile object in a property of your web page, be aware that file backed uploaded files get their temp file deleted when the event finishes. So you have to take the data or query file property and copy the file. For performance doing a hard link into your own location may be the quickest way.

The deletion of the temp file caused a few support calls in recent times where clients passed the folder items to DynaPDF or Zip/Archive functions to process them. Our functions had trouble to open the files, which is no wonder when being deleted.

Looking on the temp folder, it looks like Xojo may push an upload into a temp file. Then later processes it and copies data into smaller temp files (or hold them in MemoryBlocks). Finally the temp file deleted when the request is processed.

For the future, I'd wish they would delete the temp file in the destructor of the WebUploadedFile object, so we could keep it around a bit longer. A trick to do that may be to open the file and keep a BinaryStream for it around. On macOS and Linux you can delete an open file, so you can read it until the BinaryStream is closed and the file is really gone. On Windows you can't delete the file while it is open, so you may need to delete it yourself later.
25 04 21 - 09:24