上传的图像无法在编辑视图中编辑

时间:2015-12-02 06:13:50

标签: asp.net-mvc

我使图像上传创建控制器真的工作并保存图像为DB,更新正在工作但不替换图像,alwyas在网格视图中显示上传图像,请帮助我,我该如何解决?谢谢

创建 - 控制器

  public ActionResult Save(Component component, HttpPostedFileBase file)
    {   
        string fileName = string.Empty;
        if (file != null && file.ContentLength > 0)
        {
            // extract only the fielname
            fileName = Path.GetFileName(file.FileName);
            // store the file12 inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/Uploaded"), fileName);
            file.SaveAs(path);
        }

        var objContext = new KnittingdbContext();
        component.CreateBy = 1;
        component.StatusChangeDate = System.DateTime.Now;
        component.CreatedDate = System.DateTime.Now;
        component.EditBy = 1;
        component.EditDate = System.DateTime.Now;
        //file name added here
        component.FileName = fileName;  
        objContext.Components.Add(component);
        objContext.SaveChanges();

        TempData["Success"] = "Saved Sucessfully";

        return RedirectToAction("ComponentIndex", new { A = "New" });
    } 






public ActionResult FileName(string id)
{
    byte[] image = { };

    try
    {
        image = System.IO.File.ReadAllBytes(Server.MapPath("~/Uploaded/") + id);
    }
    catch { }

    return File(image, "image/png");
}

我的更新控制器

 public ActionResult Update(Component component, HttpPostedFileBase file)
        { 
            string fileName = string.Empty;
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(file.FileName);
                // store the file12 inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Uploaded"), fileName);
                file.SaveAs(path);


                var objContext = new KnittingdbContext();
                //blog.Id = 1;
                objContext.Components.Attach(component);
                var obJBlog = objContext.Entry(component);

                obJBlog.Property(a => a.ComponentCode).IsModified = true;
                obJBlog.Property(a => a.ComponentName).IsModified = true;
                obJBlog.Property(a => a.Remarks).IsModified = true;
                obJBlog.Property(a => a.StatusId).IsModified = true;



                objContext.SaveChanges();

                TempData["SuccessEdit"] = "Saved Sucessfully";

                return RedirectToAction("ComponentIndex");
            }
            else
            {
                return PartialView("_ComponentEdit", component);
            }
        }

更新视图

<div class="row" style="width:350px; margin-top:15px; margin-left:-10px;">
        @using (Html.BeginForm("Update", "Component", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {

            <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;"><b>Component Code</b><b style=" color:#ff0000;">*</b></div>

            <div class="col-md-6">
                @Html.TextBoxFor(a => a.ComponentCode, new { @maxlength = "5", Class = "form-control", style = "width:175px; height:25px;font-size:small;", onkeyup = "return validateChar(this)", @readonly = "readonly" })
                <div id="fname_error" style="margin-left:180px; width:140px;     top:5px; color:green; position:absolute; font-family:'Arial Unicode MS'; font-size:small;"></div>
                @Html.ValidationMessageFor(a => a.ComponentCode)
            </div>
            <div class="col-md-6"></div>    <div class="col-md-6"></div>
    <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;margin-top:5px;">
       <b> Component</b><b style=" color:#ff0000;">*</b>
    </div>
            <div class="col-md-6">
                @Html.TextBoxFor(a => a.ComponentName, new { @maxlength = "15", Class = "form-control", style = "width:175px; height:25px;margin-top:6px;font-size:small;" })
                @Html.ValidationMessageFor(a => a.ComponentName)
            </div>            <div class="col-md-6"></div>    <div class="col-md-6"></div>

        <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;">
           <b> Remarks</b>
        </div><div class="col-md-6"></div>    <div class="col-md-6"></div>
            <div class="col-md-6">
                @Html.TextBoxFor(a => a.Remarks, new { Class = "form-control", style = "width: 250px; height: 65px; resize: none;margin-top:4px;font-size:small;  " })
            </div>            <div class="col-md-6"></div>    <div class="col-md-6"></div>
    <div class="col-md-6" style="font-family:Arial, Helvetica, sans-serif; font-size:13px;">
        <b>Status</b>
    </div>

            <div class="col-md-6">
                @Html.CheckBoxFor(a => a.StatusId)
            </div>

            <div class="col-md-6">
                @Html.HiddenFor(a => a.ComponentId)
            </div>
            <div class="col-md-6"></div>    <div class="col-md-6"></div>
            <div class="col-md-6">
                <input type="submit" value="Update" class="btn btn-success" />
            </div>



    <div><input type="file" name="file" id="file" /> </div>


        }
    </div>

1 个答案:

答案 0 :(得分:2)

可能是因为您错过了更新操作中的行:

component.FileName = fileName;

以下是行动:

public ActionResult Update(Component component, HttpPostedFileBase file)
{ 
    string fileName = string.Empty;

    if (file != null && file.ContentLength > 0)
    {
        // extract only the fielname
        fileName = Path.GetFileName(file.FileName);
        // store the file12 inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/Uploaded"), fileName);

        file.SaveAs(path);

        var objContext = new KnittingdbContext();
        // blog.Id = 1;

        // update the filename
        component.FileName = filename;

        objContext.Components.Attach(component);
        var obJBlog = objContext.Entry(component);

        obJBlog.Property(a => a.ComponentCode).IsModified = true;
        obJBlog.Property(a => a.ComponentName).IsModified = true;
        obJBlog.Property(a => a.Remarks).IsModified = true;
        obJBlog.Property(a => a.StatusId).IsModified = true;

        objContext.SaveChanges();

        TempData["SuccessEdit"] = "Saved Sucessfully";

        return RedirectToAction("ComponentIndex");
    }
    else
    {
        return PartialView("_ComponentEdit", component);
    }
}