kendo-uiuploadkendo-asp.net-mvckendo-upload

How to pass rawdata from Client to API and save it as a file in Kendo Upload, calling API from kendo async Save Option


Step 1: I have implemented Kendo Upload control for uploading files and I am trying to upload using kendo async method on a button click.

    $("#files").kendoUpload({
    localization: {
        select: "Select ..."
    },
    async: {
        autoUpload: false,
        saveUrl: $('#WebAPIUrl').val() + 'File/UploadPDF/'          
    },
    multiple: false,
    select: onPdfSelect,      
    remove: onPdfRemove,        
    validation: {
        allowedExtensions: [".pdf"],
        maxFileSize: 3145728,
    },
    dropZone: ".dropZoneElement"
    }).data("kendoUpload");

Step 2: i am facing issue here, while trying to post data from kendo async call , i am not able to retrieve from webapi to save the file in a shared drive

    [HttpPost("UploadPDF")]
    public async Task<IActionResult> UploadPDF(IFormFileCollection files)
    {}

i am receiving content here in files, but not able to retrieve the file data and save as a file.


Solution

  • I found out answer after trying for 3 days. posting here as this may help some one. Basically in this scenario you don't need to do anything in the client side, data will be passed to the api from kendo async save method, from API we have to use FileStream and CopyToAsync for creating the file in a physical location

     var fileName = "xyz.pdf";
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"",fileName);
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {​​​​​​​
                await files[0].CopyToAsync(fileStream);
            }​​​​​​​