如何删除数据文件夹中的文件?

时间:2017-04-26 10:33:57

标签: asp.net gridview

我完成了代码,当我上传文件时,它将保存到文件夹调用数据中,也可以下载(我根本没有使用数据库)。当文件可以上传和下载时,我无法使删除按钮有效。有没有办法让这种情况发生?谢谢。

这是我的设计代码:

        <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
    <br />
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting">
        <Columns>
            <asp:TemplateField HeaderText="File">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("File") %>' CommandName="Download" Text='<%# Eval("File") %>'></asp:LinkButton>
                    <br />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Size" HeaderText="Size In Bytes" />
            <asp:BoundField DataField="Type" HeaderText="File Type" />
            <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
            <span onclick="return confirm('Are you sure to Delete the record?')">
            <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"></asp:LinkButton>
            </span>
            </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
        <RowStyle BackColor="White" ForeColor="#330099" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
        <SortedAscendingCellStyle BackColor="#FEFCEB" />
        <SortedAscendingHeaderStyle BackColor="#AF0101" />
        <SortedDescendingCellStyle BackColor="#F6F0C0" />
        <SortedDescendingHeaderStyle BackColor="#7E0000" />
    </asp:GridView>
    <br />

这是我的代码背后:

public partial class ScheduleUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if(FileUpload1.HasFile)
        {
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName);
        }//end if FileUpload1

        DataTable dt = new DataTable();
        dt.Columns.Add("File", typeof(string));
        dt.Columns.Add("Size", typeof(string));
        dt.Columns.Add("Type", typeof(string));


        foreach (string strFile in Directory.GetFiles(Server.MapPath("~/Data/")))
        {
            FileInfo fi = new FileInfo(strFile);

            dt.Rows.Add(fi.Name, fi.Length, GetFileTypeByExtension(fi.Extension));

        }//end foreach

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }///end btn1_Click

    private string GetFileTypeByExtension(string extension)
    {
        switch(extension.ToLower())
        {
            case ".doc":
            case ".docx":
                return "Microsoft Word Document";

            case ".xlsx":
            case ".xls":
                return "Microsoft Excel Document";

            case ".txt":
                return "Text Document";

            case ".jpg":
            case ".png":
                return "Image";

            default:
                return "Unknown";

        }//end switch
    }//end GetFileTypeByExtension

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if(e.CommandName == "Download")
        {
            Response.Clear();
            Response.ContentType = "application/octect-stream";
            Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
            Response.TransmitFile(Server.MapPath("~/Data/") + e.CommandArgument);
            Response.End();
        }//end if
    }//end GridView_RowCommand

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        File.Delete(MapPath(("~/File/")));

    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowIndex == e.RowIndex)
        {
            LinkButton lnkFile= (LinkButton)row.FindControl("LinkButton1");
            File.Delete(MapPath(("~/Data/") + lnkFile.Text)); //Assuming Data exist in your solution 
        }
    }
}