KendoUI:如何在控制器中重命名上传文件后在javascript中获取新文件名

时间:2014-04-26 15:47:06

标签: c# javascript asp.net-mvc kendo-ui kendo-upload

我有以下Kendo上传控件

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

我的控制器:

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("");
}

我的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(", ");
}

如何在我的javascript中取回“ABC.jpg”作为我的文件名而不是我选择上传的原始文件名?

1 个答案:

答案 0 :(得分:8)

通过在我的控制器中执行此操作解决:

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

并在onSuccess函数中:

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