Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validating files before upload
#1
Hello,

How do I validate files before they are uploaded. i.e. if I want to only allow files that have the word 'banana' somewhere in the filename, how do I check that before all the files are uploaded?

If the validation fails, how do I then cancel the upload? If multiple files are selected, and only one fails validation, can I just abort the upload of that one file?
Reply
#2
any advice?
Reply
#3
Here is some old code that I tried along those lines.  It worked in my simple testing.


Code:
private
    FileRejected : Boolean; 

procedure TFormUITest.IWFileUploader1AsyncSelectFile(Sender: TObject; EventParams: TStringList);
begin
  if FileRejected then
    WebApplication.ShowMessage('REJECTED!  IWFileUploader1AsyncSelectFile(Sender, '+EventParams.Text+')');
end;

procedure TFormUITest.IWFileUploader1AsyncUploadCompleted(Sender: TObject; var DestPath, FileName: string; var SaveFile, Overwrite: Boolean);
begin
  FileRejected := false;
  DestPath := WebApplication.ApplicationPath+'\wwwroot\files\uploads\';
  SaveFile := Pos('magicky',FileName)=1;
  if not SaveFile then
  begin
    FileRejected := true;
  end;
end;

procedure TFormUITest.IWFileUploader1AsyncUploadSuccess(Sender: TObject; EventParams: TStringList);
begin
//WebApplication.ShowMessage(EventParams.Text);
end;
Reply
#4
Hi Daniel,

Thank for the help. But that is not exactly what I wanted. In the case above, you are setting FileRejected to True AFTER the file is uploaded. Why go through the bother of letting them upload a 100 Mb file if I know I am going to reject it. For example, if I know I only allow files with the word "apple" somewhere in the filename, I should be able to abort the file upload before it starts if the filename does not contain the word apple.
Reply
#5
https://developer.mozilla.org/en-US/docs...input/file
Reply
#6
Hello,

While that link shows a bunch of javascript, I am not clear how that ties into the events that you expose. I presume I have to do something in the OnAsyncSelectFile, I just dont know to

a) get the filename currently "selected". Is that in the EventParms
b) how to signal to abort that file
Reply
#7
(03-19-2018, 05:57 PM)davenovo Wrote: You can use the MaxFileSize property to prevent uploading of large files.  CGDevTools has an uploader that allows you to specify permitted file extensions.  I'm not sure if it supports a file mask, but you can give it a try.
Reply
#8
(03-18-2018, 06:00 PM)davenovo Wrote: Hi Daniel,

Thank for the help. But that is not exactly what I wanted. In the case above, you are setting FileRejected to True AFTER the file is uploaded. Why go through the bother of letting them upload a 100 Mb file if I know I am going to reject it. For example, if I know I only allow files with the word "apple" somewhere in the filename, I should be able to abort the file upload before it starts if the filename does not contain the word apple.


OnAsyncSelectFile event happens BEFORE the file has been uploaded. It occurs immediately after anything has been selected at browser side, but before any update takes place. This is the exact moment to validate anything.

Besides that AllowedExtensions, MinFileSize and  MaxFileSize can limit the possible extensions and file size, without any communication to the server.
Reply
#9
Ok, so please, just tell me, if I use OnAsyncSelectFile, how do I abort the upload of the file if I decide to do so? I would expect the OnAsyncSelectFile to have a parameter FileName:string and

procedure OnAsyncSelectFile(const aFileName:string; var aAllowUpload:boolean)

However, since that is not there, can you tell me how I tell the component to stop uploading that file.  Please answer the following two questions that I have been asking

a) get the filename current file that is triggering the OnAsyncSelectFile. Is that in the EventParms?
b) how to signal to abort the upload of that file
Reply
#10
Hi, you can try this in 2 steps. In Object Inspect, set AutoUpload := False;
Put a button to Select file:
Code:
procedure TIWForm1.IWButton1_SelectAsyncClick(Sender: TObject; EventParams: TStringList);
begin
  IWFileUploader.SelectFile;
end;

Another button to upload the file after you tested the name:
Code:
procedure TIWForm1.IWButton2_SelectAsyncClick(Sender: TObject; EventParams: TStringList);
begin
  IWFileUploader.StartUpload;
end;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)