KendoUI: how to get a new file name in javascript after renaming the downloaded file to the controller

I have the following Kendo boot control

        @(Html.Kendo().Upload()
                      .Name("files")
                      .Async(a => a
                      .Save("SaveBackgroundImage", "Plans")
                      .AutoUpload(true))
                      .Multiple(false)
        .Events(events => events.Success("onSuccess")))

My controller:

public ActionResult SaveBackgroundImage(IEnumerable<HttpPostedFileBase> floorplanFiles, string floorplanId)
{
        foreach (var file in files)
        {                   
            string fileName = "ABC.jpg" //this will be random                      
            var physicalPath = Path.Combine(Server.MapPath("~/Images/Floorplans/Fullsize"), fileName);
            file.SaveAs(physicalPath);
        }
    // Return an empty string to signify success
    return Content("");
}

My javascript:

function onSuccess(e) {
    var filename = getFileInfo(e);
    alert(filename);
}

function getFileInfo(e) {
    return $.map(e.files, function (file) {
        var info = file.name;
        return info;
    }).join(", ");
}

How do I return "ABC.jpg" as my name in my javascript instead of the original file name that I choose to upload?

+4
source share
1 answer

Solved by doing this in my controller:

var newImageName = "123.jpg";
return Json(new { ImageName = newImageName  }, "text/plain");

and in function onSuccess:

function onSuccess(e) {
    var imageName = e.response.ImageName;
}
+7
source

Source: https://habr.com/ru/post/1538199/


All Articles