天天看點

ASP.NET上傳,删除檔案

   對于ASP.NET 2.0,FileUpload控件可以用來上傳檔案,首先得導入命名空間 System.IO; 利用FileUpload,指定上傳路徑,然後執行SaveAs,就可以輕松上傳,當然這隻能上傳小檔案,上傳檔案不能超過4M,這就是因為maxRequestLength的大小預設為4096,這就限制着每個請求的大小不得超過4096KB。這麼做的目的是為了保護應用程式不受惡意請求的危害。當請求超過maxRequestLength之後,ASP.NET處理程式将不會處理該請求。這裡和ASP.NET抛出一個異常是不同的,這就是為什麼如果使用者上傳檔案太大,看到的并非是ASP.NET應用程式中指定的錯誤頁面(或者預設的),因為ASP.NET還沒有對這個請求進行處理。

  下面代碼是用FileUpload進行檔案上傳的執行個體,該執行個體中檔案删除有用到與資料庫的操作,還引用了主機闆頁,請根據實際情況進行修改!

頁面背景代碼:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

public partial class Admin_Document_DocumentTemplateManage : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    //檔案上傳

    protected void btnUpload_Click(object sender, EventArgs e)

    {

        FileUpload fu = (FileUpload)(this.fvDocumentTemplatee.FindControl("fuDocumentTemplate"));

        TextBox pathText = (TextBox)(this.fvDocumentTemplatee.FindControl("DocumentPathTextBox"));

        pathText.Text = Server.MapPath("..//..//Document//DocumentTemplate//" + fu.FileName);  //将上傳路徑指派到,記得修改,哈哈!  

DocumentPathTextBox

        fu.SaveAs(pathText.Text);

        Button btnCancel = (Button)(this.fvDocumentTemplatee.FindControl("btnDelDoc"));

        btnCancel.Enabled = true;

    }

    protected void btnDelDoc_Click(object sender, EventArgs e)

    {

        TextBox pathText = (TextBox)(this.fvDocumentTemplatee.FindControl("DocumentPathTextBox"));

        File.Delete(pathText.Text);

        pathText.Text = "";

        (sender as Button).Enabled = false;

    }

    protected void fvDocumentTemplatee_ItemInserted(object sender, FormViewInsertedEventArgs e)

    {

    }

    protected void grdDocumentTemplateList_RowDeleted(object sender, GridViewDeletedEventArgs e)

    {

    }

    protected void fvDocumentTemplatee_ItemInserting(object sender, FormViewInsertEventArgs e)

    {

    }

    protected void grdDocumentTemplateList_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            e.Row.Cells[4].Attributes.Add("onclick", "javascript:return confirm('你确實要删除/"" + e.Row.Cells[1].Text + "/"嗎?')");

        }

    }

}

前台代碼:

<%@ Page Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="DocumentTemplateManage.aspx.cs" Inherits="Admin_Document_DocumentTemplateManage" Title="公文模闆資料維護" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

    <div style="text-align: center; vertical-align: top;">

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WROA %>"

            DeleteCommand="DELETE FROM [tDocumentTemplate] WHERE [DocumentTemplateID] = @original_DocumentTemplateID"

            InsertCommand="INSERT INTO [tDocumentTemplate] ([DocumentName], [DocumentDescription], [DocumentPath]) VALUES (@DocumentName, @DocumentDescription, @DocumentPath)"

            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [DocumentTemplateID], [DocumentName], [DocumentDescription], [DocumentPath] FROM [tDocumentTemplate]"

            UpdateCommand="UPDATE [tDocumentTemplate] SET [DocumentName] = @DocumentName, [DocumentDescription] = @DocumentDescription, [DocumentPath] = @DocumentPath WHERE [DocumentTemplateID] = @original_DocumentTemplateID">

            <DeleteParameters>

                <asp:Parameter Name="original_DocumentTemplateID" Type="Int64" />

            </DeleteParameters>

            <UpdateParameters>

                <asp:Parameter Name="DocumentName" Type="String" />

                <asp:Parameter Name="DocumentDescription" Type="String" />

                <asp:Parameter Name="DocumentPath" Type="String" />

                <asp:Parameter Name="original_DocumentTemplateID" Type="Int64" />

            </UpdateParameters>

            <InsertParameters>

                <asp:Parameter Name="DocumentName" Type="String" />

                <asp:Parameter Name="DocumentDescription" Type="String" />

                <asp:Parameter Name="DocumentPath" Type="String" />

            </InsertParameters>

        </asp:SqlDataSource>

        <table width="700" >

            <tr align="left">

                <td style="height: 128px">

                    新增文檔模闆</td>

            </tr>

            <tr align="left">

                <td>

    <asp:FormView ID="fvDocumentTemplatee" runat="server" DataKeyNames="DocumentTemplateID" DefaultMode="Insert" OnItemInserted="fvDocumentTemplatee_ItemInserted" Width="530px" OnItemInserting="fvDocumentTemplatee_ItemInserting" DataSourceID="SqlDataSource1" >

        <EditItemTemplate>

            DocumentTemplateID:

            <asp:Label ID="DocumentTemplateIDLabel1" runat="server" Text='<%# Eval("DocumentTemplateID") %>'>

            </asp:Label><br />

            DocumentName:

            <asp:TextBox ID="DocumentNameTextBox" MaxLength="50" runat="server" Text='<%# Bind("DocumentName") %>'>

            </asp:TextBox><br />

            DocumentDescription:

            <asp:TextBox ID="DocumentDescriptionTextBox" MaxLength="250" runat="server" Text='<%# Bind("DocumentDescription") %>'>

            </asp:TextBox><br />

            DocumentPath:

            <asp:TextBox ID="DocumentPathTextBox"  MaxLength="250" runat="server" Text='<%# Bind("DocumentPath") %>'>

            </asp:TextBox><br />

            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"

                Text="更新">

            </asp:LinkButton>

            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

                Text="取消">

            </asp:LinkButton>

        </EditItemTemplate>

        <InsertItemTemplate>

            文檔名稱:<asp:TextBox ID="DocumentNameTextBox" MaxLength="50" runat="server" Text='<%# Bind("DocumentName") %>' Width="437px"></asp:TextBox><br />

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DocumentNameTextBox"

                ErrorMessage="請輸入公文模闆名稱"></asp:RequiredFieldValidator><br />

            上傳文檔:<asp:FileUpload ID="fuDocumentTemplate" runat="server" Width="397px" />

            <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上傳" /><br />

            文檔描述:<asp:TextBox ID="DocumentDescriptionTextBox" MaxLength="250" runat="server" Text='<%# Bind("DocumentDescription") %>' Height="75px" TextMode="MultiLine" Width="442px"></asp:TextBox><br />

            文檔路徑:<asp:TextBox ID="DocumentPathTextBox" runat="server" MaxLength="250" Text='<%# Bind("DocumentPath") %>' ReadOnly="True" Width="396px"></asp:TextBox>

            <asp:Button ID="btnDelDoc" runat="server" Enabled="False" OnClick="btnDelDoc_Click"

                Text="删除" /><br />

            <asp:Label ID="lblMessage" runat="server"></asp:Label><br />

            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"

                Text="插入"></asp:LinkButton>

            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

                Text="取消"></asp:LinkButton>

        </InsertItemTemplate>

        <ItemTemplate>

            DocumentTemplateID:

            <asp:Label ID="DocumentTemplateIDLabel" runat="server" Text='<%# Eval("DocumentTemplateID") %>'>

            </asp:Label><br />

            DocumentName:

            <asp:Label ID="DocumentNameLabel" runat="server" Text='<%# Bind("DocumentName") %>'>

            </asp:Label><br />

            DocumentDescription:

            <asp:Label ID="DocumentDescriptionLabel" runat="server" Text='<%# Bind("DocumentDescription") %>'>

            </asp:Label><br />

            DocumentPath:

            <asp:Label ID="DocumentPathLabel" runat="server" Text='<%# Bind("DocumentPath") %>'>

            </asp:Label><br />

            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"

                Text="建立">

            </asp:LinkButton>

        </ItemTemplate>

    </asp:FormView>

                    <br />

    文檔模闆清單</td>

            </tr>

            <tr align="left">

                <td style="  word-break : break-all; height: 149px;">

    <asp:GridView ID="grdDocumentTemplateList" runat="server" AutoGenerateColumns="False" DataKeyNames="DocumentTemplateID" OnRowDeleted="grdDocumentTemplateList_RowDeleted" Width="530px" DataSourceID="SqlDataSource1" OnRowDataBound="grdDocumentTemplateList_RowDataBound">

        <Columns>

            <asp:BoundField DataField="DocumentTemplateID" HeaderText="DocumentTemplateID" InsertVisible="False"

                ReadOnly="True" SortExpression="DocumentTemplateID" Visible="False" />

            <asp:BoundField DataField="DocumentName" HeaderText="文檔名稱" SortExpression="DocumentName" >

                <ItemStyle Width="100px" />

            </asp:BoundField>

            <asp:BoundField DataField="DocumentDescription" HeaderText="文檔描述"

                SortExpression="DocumentDescription" >

                <ItemStyle Width="100px" />

            </asp:BoundField>

            <asp:BoundField DataField="DocumentPath" HeaderText="文檔路徑" SortExpression="DocumentPath" >   

            <ItemStyle  Wrap="True"/> 

            </asp:BoundField>

            <asp:CommandField ShowDeleteButton="True" >

                <ItemStyle Width="50px" />

            </asp:CommandField>

        </Columns>

    </asp:GridView>

                </td>

            </tr>

            <tr align="left">

                <td>

                    &nbsp;</td>

            </tr>

            <tr align="left">

                <td style="height: 25px">

                    &nbsp;</td>

            </tr>

           </table>

    </div>

    </asp:Content>

繼續閱讀