Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KendoUI: How to get new file name in javascript after renaming uploaded file in controller

I have the following Kendo upload 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 get back "ABC.jpg" as my filename in my javascript instead of the original filename that I select to upload?

like image 586
Null Reference Avatar asked Feb 13 '23 03:02

Null Reference


1 Answers

Solved by doing this in my controller:

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

and in the onSuccess function:

function onSuccess(e) {
    var imageName = e.response.ImageName;
}
like image 59
Null Reference Avatar answered Feb 15 '23 18:02

Null Reference