Handling file upload
If you have form like following snippet
<form action="/submit" method="post" enctype="multipart/form-data"> <input type="file" name="myFile"> <button type="submit">Submit</button> </form>
To retrieve file uploaded by client, you need to call
getUploadedFile() method from
IRequest interface. It returns data of type
IUploadedFileArray which array of
IUploadedFile instance. Please read Working with request for information how to work with request object.
var myFile : IUploadedFileArray; ... myFile := request.getUploadedFile('myFile');
Client can send one or more file with same name, for example:
<form action="/submit" method="post" enctype="multipart/form-data"> <input type="file" name="myFile"> <input type="file" name="myFile"> <button type="submit">Submit</button> </form>
If client upload one or more files,
length(myFile) will indicates how many
files actually uploaded.
Move uploaded file
Initially, uploaded file will be stored in system temporary directory. You need to
moveTo() methods to move into permanent location.
if (length(myFile) > 0) then begin myFile.moveTo(targetUploadPath); end;
targetUploadPath is full filename (including its target directory). You must make sure that
targetUploadPath is writeable. Exception
EInOutError is raised when
uploaded file can not be written to target path.
If you do not call
moveTo(), at the end of request handling, temporary uploaded file will be deleted.
Get uploaded file size
Size of uploaded file in octet (or bytes) can be queried with
var fSize : int64; ... fSize := myFile.size();
Get file MIME type
MIME type of uploaded file can be queried with
For example, if upload JPEG image, you may get
var mime : string; ... mime := myFile.getClientMediaType();
If client does not send any MIME type (aka Content Type) then it is assumed
Get original file name
To get original file name of uploaded file use
var filename : string; ... filename := myFile.getClientFilename();
If client does not send filename then it returns empty string.
File Upload Validation
Fano Framework validation feature provides several built-in validation rules you can use to validate against file upload, such as, to verify that field is indeed a file upload, to verify that file upload match certain MIME type or more advanced use, such as, antivirus scan validation or file format validation.
For example application that demonstrates how to handle file upload with Fano Framework, see Fano Upload. While Fano Scgi Upload example shows how to validate file being uploaded using various validation rules.