Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi everybody,

I want to delete files from the file system. I store the images in the file system and the path of the image in the database. I can delete the path from the database. But not from the filesystem.

I do it like this:


C#
public ActionResult DeleteFiles(int id, string photoFilename)
       {

           //string photoFileName;
           ViewBag.deleteSuccess = "false";
           var photoName = "";
           photoName = photoFilename;
           string fullpath = Request.MapPath("~/Images/profile" + photoName);


           if (System.IO.File.Exists(fullpath))
           {
               System.IO.File.Delete(fullpath);

           }

           LolaBikePhoto lola = db.lolabikerPhotos.Find(id);
           db.lolabikerPhotos.Remove(lola);

           db.SaveChanges();


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


But photoName is always null.

Thank you


And this is my view:

C#
foreach (var item in Model.LolaBikePhotos)
        {
            <table>
                <tr>
                    <td>

                        <br />
                        <img src="/Images/profile/@item.ImagePath" alt="" height=150 width=200 />
                        @Html.ActionLink("Delete", "DeleteFiles", "Account", new { id = @item.PhotoID }, null)

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

            @*<input type="submit" value="Delete" />*@


        }


The fullpath is G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\profile?? but where is then the name of the actual picture that you want to delete. after profile.. has to be the name of the picture.
Posted
Updated 27-Jan-15 23:52pm
v3
Comments
_Asif_ 28-Jan-15 5:31am    
Files are present in /Images/Profile folder?
[no name] 28-Jan-15 5:41am    
Hi , thank you for your replay.Yes they are. Deleteing path from db works, but the image stays in the file system. That is the problem

In your view you only pass an id into your DeleteFiles function which is why photoFilename is null:
@Html.ActionLink("Delete", "DeleteFiles", "Account", new { id = @item.PhotoID }, null)


Two suggested options:
1) Add the ImagePath to the parameters you're passing:
@Html.ActionLink("Delete", "DeleteFiles", "Account", new { id = @item.PhotoID, photoFilename=@item.ImagePath }, null)


2) Locate the database item in your DeleteFiles first via its ID and then extract the path from that. That way you can drop the photoFilename parameter entirely.
 
Share this answer
 
Comments
[no name] 28-Jan-15 6:12am    
Thank you Al_Brown!! that was it.
BillWoodruff 28-Jan-15 10:53am    
+5
You will need permissions to access the folder on the client machine.
 
Share this answer
 
Comments
[no name] 28-Jan-15 5:42am    
I can save the images. So I have acces to that folder

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900