Click here to Skip to main content
12,948,130 members (89,853 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all i am working on mvc3

here i need to delete a previously uploaded file from the sessions data

anh i am displaying the file before inserting into data base so i am displaying the data in sessions now i need to delete the previously selected file plz help to how to get the selected file index value to delete the file from the sessions

For example here check this post it is in c# but i need this is in mvc3 please help me to do this work plz help me anyone

here my models are
using System;
  using System.Collections.Generic;
   using System.Linq;
  using System.Web;
   using System.ComponentModel.DataAnnotations;
   namespace BugTracker.Models
public class BugModel

    public BugModel()
        if (ListFile == null)
            ListFile = new List<BugAttachment>();
    public List<BugAttachment> ListFile { get; set; }
    public string ErrorMessage { get; set; }
public class BugAttachment
    public string FileName { get; set; }
    public int BugAttachmentID { get; set; }
    public string AttachmentName { get; set; }
    public int BugID { get; set; }
    public string AttachmentUrl { get; set; }
    public string AttachedBy { get; set; }

here my controllers
> public ActionResult UploadFile(string AttachmentName, BugModel model)
        BugModel bug = null;
        if (Session["CaptureData"] == null)
            bug = model;
            bug = (BugModel)Session["CaptureData"];
        foreach (string inputTagName in Request.Files)
            HttpPostedFileBase file1 = Request.Files[inputTagName];
            if (file1.ContentLength > 0)
                BugAttachment attachment = new BugAttachment();
                var allowedExtensions = new[] { ".doc", ".xlsx", ".txt", ".jpeg", ".docx" };
                var extension = Path.GetExtension(file1.FileName);
                if (!allowedExtensions.Contains(extension))
                    model.ErrorMessage = "{ .doc, .xlsx, .txt, .jpeg }, files are allowed.... ";
                    string filename = Guid.NewGuid() + Path.GetFileName(file1.FileName);
                    string path = "/Content/UploadedFiles/" + filename;
                    string savedFileName = Path.Combine(Server.MapPath("~" + path));
                    attachment.FileName = "~" + path.ToString();
                    attachment.AttachmentName = AttachmentName;
                    attachment.AttachmentUrl = attachment.FileName;
                    model = bug;
                Session["CaptureData"] = model;
        return View("LoadBug", bug);

and here my view page
<div class="UploadMain">    <%:Html.Label("Attachment Name:") %>
    <%:Html.TextBoxFor(model=>model.AttachmentName) %>
        <%:Html.Label("Upload Files") %></span>
    <input type="file" name="file" id="file" />
    <input type="submit" value="Upload" id="Upload" class="style-name cancel"  />
    <%--onclick="window.location.href='<%= Url.Action("UploadFile", "Bug") %>';"--%>
    <table align="center" class="gridtable" border="0" cellspacing="0" cellpadding="0">
                Attachment Name
                Attachment Url
        <% if (Model != null && Model.ListFile != null)
           {  %>
        <% foreach (var Emp in Model.ListFile)
           { %>
        <tr class="Data">
                <%:Emp.AttachmentName %>
                <%: Emp.FileName %>
          <%-- <%= Html.ActionLink("Delete", "Delete")%>--%>
            <%:Html.ActionLink("Delete", "Delete", new { @FileName = Emp.FileName })%>

        <% } %>
        <% } %>
Posted 31-Aug-12 1:45am
Updated 31-Aug-12 9:27am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You generate a guid for the file. Add it to the model, and use it in the view to call the delete action. You should store both the original and the generated file name. And I would not store the path to the folder, just the name. The path is a configuration parameter, must not be hardcoded.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,344
CHill60 3,275
Maciej Los 2,703
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100