This tutorial will show how we can upload files to a web server and also use a GridView control to delete files as well. VB version.
Quite often, we want to allow uploading of files to a web server, which enables new content to be loaded much quicker & easier than using FTP. ASP.NET makes this easy for us to do, but what happens when we want to delete files from a web server, too? Well ASP.NET, of course, has an answer for this too. We can use a GridView to display files in a certain directory, and allow a user to delete these files.
First, we need the following assembly reference:
| Imports System.IO |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running 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 /> </form> <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%" /> </asp:TemplateField><ItemTemplate> <asp:HyperLink ID="FileLink" </ItemTemplate>NavigateUrl='<%# "media/ul/" + Container.DataItem.ToString() %>' Text='<%# Container.DataItem.ToString() %>' runat="server" Target="_blank" /> <asp:TemplateField HeaderText="Delete?"> <ItemStyle HorizontalAlign="Center" Width="30%" /> </asp:TemplateField><ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" OnClientClick='return confirm("Are you sure you want to delete this entry?");' </ItemTemplate>Text="Delete?" /> </Columns> </asp:GridView> |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
The code-behind will look something like this:| Imports System.IO Partial Class _Default Inherits System.Web.UI.Page End Class 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) End SubIf (Not IsPostBack) Then GridView1.DataBind() End IfProtected Function GetUploadList() As String() Dim folder As String = Server.mapPath("/media/ul") End FunctionDim 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 iReturn fileNames Protected Sub UploadThisFile(ByVal upload As FileUpload) If upload.HasFile Then End SubDim theFileName As String = Path.Combine(Server.mapPath("/media/ul"), upload.FileName) End Ifupload.SaveAs(theFileName) labelStatus.Text = "<b>File has been uploaded.</b>" Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting e.Cancel = True End SubDim 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() Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click UploadThisFile(FileUpload1) End SubGridView1.DataBind() |
No comments:
Post a Comment