Search code examples
asp.net-mvcasp.net-corekendo-asp.net-mvcrazor-pages

kendo UI asynchronous uplaod on Razor Page returns 404


I am trying to use Kendo UI async upload on Razor Page (No controller) But I get 404 error

Index.cshtml page-

<div class="row">
    <div class="">
        <form asp-action="" class="" id=""  enctype="multipart/form-data">
            <div class="form-group">
                <label class="">Review Type</label>
                <div class="">               
                    <select asp-for="ReviewType" asp-items="@(new SelectList(Model.ReviewTypes, "ReviewTypeLookupId", "ReviewTypeName"))" class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <label class=""></label>
                <div class="">
                    @(Html.Kendo().Upload()
                          .Name("files")
                          .Async(a => a
                            .Save("Index?handler=Save", "UploadManagement")
                              .Remove("Remove", "UploadManagement/Index")
                              .AutoUpload(true)
                          )
                          )
                </div>
            </div>
            <div class="form-group">
                <button type="submit" id="submit-all" class="btn btn-default">Upload </button>
            </div>
        </form>
    </div>  

Index.cshtml.cs Page

 [HttpPost]
        public ActionResult OnPostSave(IEnumerable<IFormFile> files)
        {
            // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    //var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

                    //// Some browsers send file names with full path.
                    //// We are only interested in the file name.
                    //var fileName = Path.GetFileName(fileContent.FileName.Trim('"'));
                    //var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);

                    //// The files are not actually saved in this demo
                    ////file.SaveAs(physicalPath);
                }
            }

            // Return an empty string to signify success
            return Content("");
        }

Error - Failed to load resource: the server responded with a status of 404 (Not Found)


Solution

  • The easiest way to solve this, is to not use .Save(string action, string controller) or any overload of this, but .SaveUrl(string url):

    @(Html.Kendo().Upload()
        .Name("files")
        .Async(a => a
            .SaveUrl("./Index?handler=Save")
            .AutoUpload(true)
    ))
    

    This will also work, if you are in a non-default area and the url to the page itself is acually /area-url/Index?handler=foo