上传图片后删除图片

时间:2014-10-24 13:36:03

标签: c# asp.net-mvc-4 visual-studio-2013 entity-framework-4

我想在上传后删除图片。上传后图片显示。但是现在用户还必须获得删除图像的机会但是如果我选择一个图像并按下删除没有任何反应我将其用于查看:

<div id="tabs-3">


           @using (Html.BeginForm("UploadMorePhotos", "Account", FormMethod.Post, new { enctype = "multipart/form-data" }))
           {
               @Html.AntiForgeryToken()


            <div class="form-field">
                <p>Select pictures:</p>
                <div class="upload-container">
                    <div class="upload">
                        <input type="file" name="files" value="" multiple="multiple" id="file1" />

                        <img src="@Url.Content("~/images/deleteButton.png")" alt="Remove picture." />
                    </div>
                </div>
            </div>

            <div class="form-field">
                <input type="submit" value="Upload" />
            </div>

    <br />

      foreach (var item in Model.LolaBikePhotos )
       {
        <img src="~/Images/profile/@item.ImagePath" alt="" height=150 width=200 />

           using (Html.BeginForm("Account", "DeleteFiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
           {
            <table>
                <tr>
                    <td></td>
                    <td>File Name</td>
                </tr>


                    <tr>
                        <td>
                            <input type="checkbox" name="files" value="@item.ImagePath" />
                        </td>
                        <td>@item.ImagePath</td>
                    </tr>
                            </table>

            <input type="submit" value="Delete" />
           }




       }





           }

       <br />


        </div>

这是我的行动方法:

 [HttpPost]
        public ActionResult DeleteFiles(IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var id in files)
            {
                var fileName = Path.GetFileName(id.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/profile" + @"\" + fileName.Replace('+', '_')));
                var file = db.lolabikerPhotos.Find(id);

                // you now have access to the information of the file, including path, name, etc

                // delete file using your filepath (path + filename)
               System.IO.File.Delete(path);
            }


            return Redirect(Url.Action("Edit", "Account") + "#tabs-3");

        }

0 个答案:

没有答案