Search code examples
javascriptasp.net-mvcrazorkendo-upload

Kendo UI multiple files upload issue


I am using a Kendo File Upload control to upload multiple files. Only few of the files are getting uploaded (especially first and last) or some random ones. Is there any solution for this ?

Index.cshtml :

<input name="files" id="files" type="file" multiple="multiple" />

JS File :

$("#files").kendoUpload
    ({
        async: {
            saveUrl: "/Controller/GetUserUploadedFiles",
            removeUrl: "/Controller/Remove",
            autoUpload: false,
            allowmultiple: true
        },
        select: function (e) {
            onSelect(e);
        },
        success: function (e) {
        },
        error: function (e) {
        }
    });

//Controller Method

[HttpPost]
   public void GetUserUploadedFiles(IEnumerable<HttpPostedFileBase> files)
   {   
     //Custom logic here
   }

Also, it would be great if i can get all the files as Enumerable in one controller method call rather than having it called multiple times for multiple files.

Is there anything i am missing or doing wrong ?

Thanks, Srini


Solution

  • This code will upload all the files that were selected in Kendo Upload, and then run code on each.

    [HttpPost]
    public void GetUserUploadedFiles()
    {   
        Request.Content.LoadIntoBufferAsync().Wait();
        var result = Task.Factory
                     .StartNew(() => Request.Content.ReadAsMultipartAsync().Result,
                      CancellationToken.None,
                      TaskCreationOptions.LongRunning,
                      TaskScheduler.Default).Result;
    
        var contents = result.Contents;
        HttpContent httpContent = contents.First();        
    
        Stream file = httpContent.ReadAsStreamAsync().Result;
    
        if (file.CanRead)
        {
            // Code that will be executed on each file
        }
    }
    

    You can get the filename by using:

    string filename = httpContent.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
    

    You can get the uploaded file media type by using:

    string uploadedFileMediaType = httpContent.Headers.ContentType.MediaType;