如何基于mvc3中的索引值删除以前选择的文件? [英] how to delete previusly selected file based on index value in mvc3?

查看:89
本文介绍了如何基于mvc3中的索引值删除以前选择的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在研究mvc3

在这里,我需要从会话数据中删除先前上传的文件

anh我在插入数据库之前正在显示文件,所以我现在在会话中显示数据.我需要删除先前选择的文件,请帮助获取如何从会话中删除文件的所选文件索引值

例如,在此处检查此帖子是否在c#中,但我需要在mvc3中使用此帖子,请帮助我完成此工作,请帮助我任何人

这是我的模特

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;
        }
        else
        {

            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.... ";
                }
                else
                {
                    string filename = Guid.NewGuid() + Path.GetFileName(file1.FileName);
                    string path = "/Content/UploadedFiles/" + filename;
                    string savedFileName = Path.Combine(Server.MapPath("~" + path));
                    file1.SaveAs(savedFileName);
                    attachment.FileName = "~" + path.ToString();
                    attachment.AttachmentName = AttachmentName;
                    attachment.AttachmentUrl = attachment.FileName;
                    bug.ListFile.Add(attachment);
                    model = bug;
                }
                Session["CaptureData"] = model;
            }

        }
        ModelState.Clear();

        return View("LoadBug", bug);
    }


这是我的查看页面


and here my view page

<div class="UploadMain">    <%:Html.Label("Attachment Name:") %>
    <%:Html.TextBoxFor(model=>model.AttachmentName) %>
    <span>
        <%: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">
        <tr>
            <th>
                Attachment Name
            </th>
            <th>
                Attachment Url
            </th>
            <th>
            Action 
            </th>
        </tr>
        <% if (Model != null && Model.ListFile != null)
           {  %>
        <% foreach (var Emp in Model.ListFile)
           { %>
        <tr class="Data">
            <td>
                <%:Emp.AttachmentName %>
            </td>
            <td>
                <%: Emp.FileName %>
            </td>
           <td>
          <%-- <%= Html.ActionLink("Delete", "Delete")%>--%>

            <%:Html.ActionLink("Delete", "Delete", new { @FileName = Emp.FileName })%>


            </td>
        </tr>
        <% } %>
        <% } %>
    </table>
</div>

推荐答案

您将为文件生成一个GUID.将其添加到模型中,并在视图中使用它来调用delete操作.您应该同时存储原始文件名和生成的文件名.而且我不会存储该文件夹的路径,而只存储名称.该路径是一个配置参数,不能进行硬编码.
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.


这篇关于如何基于mvc3中的索引值删除以前选择的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆