Flash File Uploader

The Morfik FlashUploader package contains a single widget (FlashFileUploader) that allows files to be submitted to a web site. Morfik version 3 no longer contains the FlashFileUploader component but this package adds in the FlashFileUploader widget.


package-view.png
package-down.png


A Quick Walkthrough

To use the Morfik FlashUploader package simply add the package to the project by utilizing the “Used Packages” command on the project ribbon (see Figure 1 Used Packages Command), or simply drag the package file onto the application main client area. Once added [a single widget] will appear on the home ribbon when the form designer is active (see Figure 2 Widgets List). These widgets can be placed on application forms and provide immediate functionality with minimal coding.


security-fig1.png
Figure 1: Used Packages Command


FlashUploader.jpg
Figure 2: Widgets List


After adding the package to your project, to use it just drop a FlashFileUploader widget onto a form to get something that looks like Figure 3 The FlashFileUploader Widget.


FlashUploader_1.jpg
Figure 3: The FlashFileUploader Widget


Once on the form, a DataField can be set for the widget in the Property Inspector (see below if no data field is set). Also note that Max File Size can also be set in the Property Inspector, however to allow file sizes large than 2 megabytes, code must be added to the XApp.OnStart event, as seen below:

FX Code

Procedure XApp.XAppStart(Sender: TObject);
Begin
    Catalog.PostLimits.AddLimit('uplv2', 10 * 1024 * 1024);   { 10 MBs }
End;


If this PostLimit is not changed and a file that is too large is uploaded, it will appear to upload but will not. On the other hand, the FlashFileUploader widget will not even accept a file that is larger than the value set in the Property Inspector.


If a DataField is Not Specified

If a datafield is not assigned to the widget, code must be added to the project module to handle the file upload. This should be done by implementing the BeforeUploadFinalize event as illustrated in the code below:

FX Code

Procedure TestXApp.XAppBeforeUploadFinalize(UploadData: TUploadData; Response: THTTPResponse; Var DoProceed: Boolean);
Var
    gAppPath: String;
Begin
    gAppPath := ExtractFileDir(Catalog.ModuleFileName) + FileSeparator;
    { Only execute the following code if the file is not assigned to a data field }
    If UploadData.Param['MFK$KEY_FIELD_VALUE'] = '' Then
    Begin
        If Not FolderExists(gAppPath + 'Images') Then
            CreateFolder(gAppPath + 'Images');
        UploadData.DataStream.SaveToFile('Images\' + UploadData[cFileUploadNameParam]);
    End;
End;


Since this event is fired on any file that is uploaded, even files that are assigned to a datafield, there is a check to make sure that no datafield has been set for the widget.


Handling Events on the Browser Side

The following events are fired on the browser side for the FlashFileUploader widget: OnFileQueued, OnUploadSuccess and OnUploadComplete. All events have the same signature, as seen below:

FX Code

    Procedure Queued(Sender: TObject; FileInfo : TFlashUploadFileInfo);
    Procedure UploadSuccess(Sender: TObject; FileInfo : TFlashUploadFileInfo);
    Procedure UploadComplete(Sender: TObject; FileInfo : TFlashUploadFileInfo);


Each of these event handlers can be assigned at design time or can be assigned at runtime as seen below: FX Code

Procedure Form1.WebFormReady(Var Ready: Boolean);
Begin
    mfk_FlashFileUploader1.OnFileUploadSuccess := @UploadSuccess;
    mfk_FlashFileUploader1.OnFileUploadComplete := @UploadComplete;
End;


Note that the UploadSuccess event will fire prior to the UploadComplete event. Therefore if the UploadComplete event is detected without an UploadSuccess event, the upload was not completed successfully. The TFlashUploadFileInfo class contains the members - id (String), index (Integer), name (String), size (Integer), "type" (String), creationdate (TDateTime), modificationdate (TDateTime) and filestatus (Integer) that can be used, for example, to determine if a file can be uploaded, as in the example below:

FX Code

Procedure Form1.mfk_FlashFileUploader1FileQueued(Sender: TObject; FileInfo: TFlashUploadFileInfo);
Begin
    If Not StringsEqual('csv', FileInfo."type") Then
    Begin
        If Not ConfirmAction('Are you sure this is the file you want to upload?') Then
            mfk_FlashFileUploader1.ClearQueue;
    End;
End;


See Also

Back to top