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

How to pass files as parameter to MVC method without submitting the form

1 Answer 627 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Daochuen
Top achievements
Rank 1
Iron
Veteran
Iron
Daochuen asked on 14 Jan 2020, 10:17 PM

Hello,

I used kendo upload widget in my project MVC project. How to pass the files through ajax to MVC method without submitting the form?  I tried couple of ways but failed to get the files. Could someone help? Please see my sample code below:

 

---------In .cshtml file -----------

            <div >

                @(Html.Kendo().Upload()

                        .Name("files")

                        .Multiple(false)

                        .HtmlAttributes(new { aria_label = "files" })

                        .Validation(validation => validation.AllowedExtensions(new string[] {".xlsx" }))

                )

 

            @(Html.Kendo().Button()

            .Name("UploadFile")

            .Content("Process Files")

            .Events(event => event.Click("onUploadFile "))

            )

             </div>

 

------In . js file:-------

 

Function onUploadFile(){

        var upload = $("#files").data("kendoUpload"),

          files = upload.getFiles();

 

      $.post("TestMethod" ,

      { files: files },  <-----------------------------------I try to pass parameter here.

     function (data, status) {...}

      );

}

 

------In server side:-------

  public JsonResult TestMethod (IEnumerable<HttpPostedFileBase> files)

        {  ....

            return Json(null, JsonRequestBehavior.AllowGet);

        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Petar
Telerik team
answered on 16 Jan 2020, 12:01 PM

Hi Daochuen,

You can use the Asynchronous Mode of the Upload component. When working it this mode, the selected files are immediately uploaded to the server.

This demo project demonstrates how the Upload component can be configured to work in asynchronous mode in the ASP.NET MVC context. 

You can also find useful the Upload component API and more specific its async properties. If you want to use the jQuery version of the Upload component, here is the linked above demo realized with jQuery.

Regards,
Petar
Progress Telerik

Get quickly onboarded and successful with your Telerik UI for ASP.NET MVC with the dedicated Virtual Classroom technical training, available to all active customers.
Tags
Upload
Asked by
Daochuen
Top achievements
Rank 1
Iron
Veteran
Iron
Answers by
Petar
Telerik team
Share this question
or