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>

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值