Kendo UI - 通过网格中的上传小部件将图像路径存储在数据库中

时间:2014-04-28 04:09:48

标签: c# javascript kendo-ui kendo-grid kendo-upload

我可以上传图像并将其保存到文件夹,但唯一的问题是将路径存储在SQL表中。我想将Photo路径保存到数据库,以便可以在其他地方显示。这是我到目前为止所做的。

项目类:

public class Item
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public string Photo { get; set; }
}

这是控制器的更新操作:

[HttpPost]
public ActionResult Update(item, HttpPostedFileBase files)
{
    if (files != null && files.ContentLength > 0)
        {
            string fileName = Path.GetFileName(files.FileName);
            var physicalPath = Path.Combine(Server.MapPath("~/Content/uploads"), fileName);

            item.Photo = physicalPath;
            files.SaveAs(physicalPath);

            return Json(new { Photo = fileName }, "text/plain");
        }

    return Json(new[] { item });
}

带有上传小部件集成的网格示例:
http://jsbin.com/safog/1

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,它的工作正常。

           [HttpPost]
           public ActionResult Update(item, HttpPostedFileBase files)
            {
             if (files != null && files.ContentLength > 0)
               {
                string fileName = Path.GetFileName(files.FileName);
                 var physicalPath = path.Combine(HttpContext.Request.MapPath("~/Content/uploads"), fileName);
                 item.Photo = physicalPath;
                 files.SaveAs(physicalPath);
                 return Json(new { Photo = fileName }, "text/plain");
              } 
         return Json(new[] { item });
           }
相关问题