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

问题描述 投票:0回答:1

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

物品类别:

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

这是控制器的Update操作:

[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

c# javascript kendo-ui kendo-grid kendo-upload
1个回答
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 });
           }
© www.soinside.com 2019 - 2024. All rights reserved.