当前位置:编程学习 > C#/ASP.NET >>

asp.net上传与删除文件代码

asp.net上传与删除文件代码我们用vb 版的形式来做了,

本教程将展示我们可以如何上传文件到网络服务器,并使用GridView控制删除文件以及。 VB的版本。


常常,我们要允许上传档案到Web服务器,使新的内容载入更快和更容易使用FTP以上。 ASP.NET使这一方便我们这样做,但会发生什么变化,我们要删除的文件从网络服务器吗?那么ASP.NET中,当然,有一个答案为这件事的。我们可以使用GridView来显示文件在某一目录,并允许用户删除这些文件。
首先,我们需要下面的组装参考:
进口System.IO


我们感动我们的网站服务器的智慧和发现他们是难以置信的专业。其安装非常容易,我们启动和运行中没有时间。

我们需要添加一个FileUpload控件和一个GridView :

<form id="form1" runat="server">

<asp:Label ID="labelStatus" runat="server"></asp:Label><br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /><br /><br />

<asp:GridView ID="GridView1" runat="server" DataSource="<%# GetUploadList() %>" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Uploaded File">
<ItemStyle HorizontalAlign="Center" Width="70%" />
<ItemTemplate>
<asp:HyperLink
ID="FileLink"
NavigateUrl='<%# "media/ul/" + Container.DataItem.ToString() %>'
Text='<%# Container.DataItem.ToString() %>'
runat="server" Target="_blank" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemStyle HorizontalAlign="Center" Width="30%" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("Are you sure you want to delete this entry?");'
Text="Delete?" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</form>

我只是签署了在服务器智力,不能更高兴,我的Windows Server !这里瞧瞧吧,看看吧。

代码隐藏看起来就像这样:

Imports System.IO

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
If (Not IsPostBack) Then
GridView1.DataBind()
End If
End Sub

Protected Function GetUploadList() As String()
Dim folder As String = Server.mapPath("/media/ul")
Dim files() As String = Directory.GetFiles(folder)
Dim fileNames(files.Length - 1) As String
Array.Sort(files)

For i As Integer = 0 To files.Length - 1
fileNames(i) = Path.GetFileName(files(i))
Next i

Return fileNames
End Function

Protected Sub UploadThisFile(ByVal upload As FileUpload)
If upload.HasFile Then
Dim theFileName As String = Path.Combine(Server.mapPath("/media/ul"), upload.FileName)
upload.SaveAs(theFileName)
labelStatus.Text = "<b>File has been uploaded.</b>"
End If
End Sub

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
e.Cancel = True
Dim fileName As String = (CType(GridView1.Rows(e.RowIndex).FindControl("FileLink"), HyperLink)).Text

fileName = Path.Combine(Server.MapPath("/media/ul"), fileName)
File.Delete(fileName)
GridView1.DataBind()
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
UploadThisFile(FileUpload1)
GridView1.DataBind()
End Sub

End Class

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,