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

Kendo UI - Store image path in database through upload widget in grid

本文关键字:图像 路径 数据库 存储 小部 UI- 网格 Kendo      更新时间:2023-09-26

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

物品类别:

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

尝试使用下面的代码,它可以正常工作。

           [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 });
           }