We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Capturing invalid uploads

I have the following properties set:

                AllowedExtensions=".xlsx, .xls"
AllowMultiple=false
ShowFileList=false
MinFileSize="10000"
MaxFileSize="614400"

Since I'm hiding the file list, we don't get to see the messages when the file size or type is disallowed or if they tried to upload more than one file at once.

How can I trap for those so I can display my own error messages? I certainly don't want to just ignore the upload without giving the user some feedback. But I also don't need or want the file list.



4 Replies

KA Keith A Price February 17, 2023 07:40 PM UTC

On further research, I found this:

        void OnBeforeUpload(BeforeUploadEventArgs args)

        {
            Console.WriteLine(args.FilesData[0]...)
        }


Does BeforeUpload actually trigger before the file is sent to the server? If you don't provide a way for us to show status messages when min/max or file type or multiple files is attempted, then maybe I turn those restrictions off and put them in BeforeUpload? Then I can do my own checks and messages.


Let



UD UdhayaKumar Duraisamy Syncfusion Team February 20, 2023 04:33 PM UTC

You can use the FileSelected event to know the file upload restricted message, such as file type and file size. To indicate to the user whether the file was uploaded successfully or not, you can use the Success and OnFailure events.


Please refer to the code snippet and sample below for more information.

<div style="margin:130px auto;width:300px">

    @if (ErrorMessage != null && ErrorMessage != "Ready to upload")

    {

        <p>@ErrorMessage</p>

    }

 

    <SfUploader AllowedExtensions=".xlsx, .xls"

                AllowMultiple=false

                ShowFileList=false

                MinFileSize="10000"

                MaxFileSize="614400">

        <UploaderEvents FileSelected="@FileSelectedHandler" OnFailure="@OnFailureHandler" Success="@SuccessHandler"></UploaderEvents>

    </SfUploader>

</div>

@code {

    private string ErrorMessage;

    private void FileSelectedHandler(SelectedEventArgs args)

    {

        if (args.FilesData.Count != 0)

        {

            ErrorMessage = args.FilesData[0].Status;

        }

 

    }

    private void OnFailureHandler(FailureEventArgs args)

    {

        ErrorMessage = args.File.Status;

    }

    private void SuccessHandler(SuccessEventArgs args)

    {

        ErrorMessage = args.File.Status;

    }

}


  • FileSelectedHandler is triggered when the user selects a file to upload. The function checks if file was selected, and if so, it assigns the status of the first file to the ErrorMessage variable.
  • OnFailureHandler is triggered when an error occurs during the upload process. The function assigns the status of the failed file to the ErrorMessage variable.
  • SuccessHandler is triggered when a file is successfully uploaded. The function assigns the status of the uploaded file to the ErrorMessage variable.

All three functions update the ErrorMessage variable, which is then used to display any error messages in the code block inside the if statement in the HTML code. If there are no errors, the ErrorMessage variable will be set to "Ready to upload", which will not be displayed.