asp.net upload and delete file code we use the form of VB version to do,
This tutorial will show how we can upload files to a Web server and use the GridView control to delete files as well. The version of VB.
Often, we want to allow uploading files to the Web server so that new content is loaded faster and easier to use with FTP above. Asp. NET makes this convenient for us to do, but what will happen to the files we want to delete from the Web server? So asp.net, of course, there's an answer for this thing. We can use the GridView to display files in a directory and allow users to delete those files.
First, we need the following assembly reference:
Import System.IO
We moved our web server to the wisdom and found that they were incredibly professional. Its installation is very easy, we start and run in no time.
We need to add a FileUpload control and a 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 your sure you want to delete this entry?"); '
Text= "Delete?"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
I just signed up in the server intelligence and couldn't be happier with my Windows Server! Here, take a look at it.
Code-behind looks like this:
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 the 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