This is a migrated thread and some comments may be shown as answers.

Filename, Error events and validation with synchronous mode

1 Answer 163 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Kerry
Top achievements
Rank 1
Kerry asked on 11 Jun 2020, 08:35 PM
Is it possible to have Filename, Error events and validation with synchronous mode? I have a form where i need the upload to happen on form submit for the additional information and would like these options. Is it possible? 

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 16 Jun 2020, 11:14 AM

Hello Kerry,

The built-in error event is available only when the Telerik UI for Asp.NET Core Upload is in asynchronous mode. If you would like to use it in synchronous mode I suggest reviewing the following posts for possible approach on handling HTML Form Data in ASP.NET Web API:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2

The synchronous mode does not prevent the use of File Validation. You could still initialize the Upload in the following manner to allow only the upload of images, for example, and apply image size validation:

<form method="post" action='@Url.Action("Submit")'>
    <div class="demo-section k-content">
        @(Html.Kendo().Upload()
         .Name("images")
         .Validation(validation => validation
             .AllowedExtensions(new string[] { ".gif", ".jpg", ".png" })
             .MaxFileSize(31457280)
             .MinFileSize(30720)
         )
        )
        <p style="padding-top: 1em; text-align: right">
            <button type="submit" class="k-button k-primary">Submit</button>
        </p>
    </div>
</form>

In the above example, the action method should expect a parameter named images. You can still obtain the information on file name as demonstrated in the Upload Basic Usage demo:

public ActionResult Submit(IEnumerable<IFormFile> images)
{
            IEnumerable<string> fileInfo = new List<string>();

            if (images != null)
            {
                fileInfo = GetFileInfo(images);
            }

            // custom logic on file saving/handling
}

private IEnumerable<string> GetFileInfo(IEnumerable<IFormFile> files)
        {
            List<string> fileInfo = new List<string>();

            foreach (var file in files)
            {
                var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
                var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));

                fileInfo.Add(string.Format("{0} ({1} bytes)", fileName, file.Length));
            }

            return fileInfo;
        }

Regards,
Aleksandar
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Upload
Asked by
Kerry
Top achievements
Rank 1
Answers by
Aleksandar
Telerik team
Share this question
or