How to pass selected files in Kendo Upload as parameter in ajax request

后端 未结 2 2048
我寻月下人不归
我寻月下人不归 2021-01-22 15:20

After much of struggle im posing this question. Im using a Kendo Upload on a page. Am able to post the selected files on the asyn mode with whe the page has Html.BeginForm. But

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 16:02

    If your view is based on a model and you have generated the controls inside

    tags, then you can serialize the model to FormData using:

    
    

    This will also include any files generated with: and post it back using:

    
    

    and in your controller:

    [HttpPost]
    public ActionResult YourActionName(YourModelType model)
    {
    }
    

    or (if your model does not include a property for HttpPostedFileBase)

    [HttpPost]
    public ActionResult YourActionName(YourModelType model, 
    HttpPostedFileBase myImage)
    {
    }
    

    If you want to add additional information that is not in the form, then you can append it using

    
    


    Example Usage :

    View :

    @using (Html.BeginForm("Create", "Issue", FormMethod.Post,
    new { id = "frmCreate", enctype = "multipart/form-data" }))
    { 
        @Html.LabelFor(m => m.FileAttachments, new { @class = "editor-label" })
        @(Html.Kendo().Upload()
            .HtmlAttributes(new { @class = "editor-field" })
            .Name("files")
        )
    }
    
    
     
    


    Controller :

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Exclude = null)] Model viewModel, IEnumerable files)
    {
        //code omitted for brevity
        return Json(new { success = false, message = "Max. file size 10MB" }, JsonRequestBehavior.AllowGet);
    }
    

    Hope this helps...

提交回复
热议问题