Asp.net file upload and deletion code

Source: Internet
Author: User

Asp.net upload and delete file code is implemented in the form of vb. We need to allow the upload of files to the Web server so that new content can be loaded faster and more easily over FTP.

We have used the vb version to upload and delete file code in asp.net,

This tutorial will show you how to upload files to the network server, and use the GridView control to delete files as well. VB version.


Often, we need to allow the upload of files to the Web server so that new content can be loaded more quickly and more easily over FTP. ASP. NET makes this easier for us to do this, but what changes will happen to the files we want to delete from the network server? In ASP. NET, of course, there is an answer to this question. You can use the GridView to display files in a directory and allow users to delete these files.
First, we need the following Assembly reference:
Import System. IO


We are touched by the wisdom of our website servers and discover that they are incredibly professional. It is very easy to install. We have no time to start and run it.

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 = "button#click"/> <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: HyperLinkID = "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>

I just signed up on Server intelligence and cannot be happier, my Windows Server! Let's take a look.

Code hiding 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) ThenGridView1.DataBind () End IfEnd 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 fileNamesEnd Function

Protected Sub UploadThisFile (ByVal upload As FileUpload) If upload. HasFile ThenDim theFileName As String = Path. Combine (Server. mapPath ("/media/ul"), upload. FileName)
Upload. SaveAs (theFileName)
LabelStatus. Text = "<B> File has been uploaded. </B>" End IfEnd Sub

Protected Sub GridView1_RowDeleting (ByVal sender As Object, ByVal e As System. Web. UI. WebControls. GridViewDeleteEventArgs) Handles GridView1.RowDeletinge. 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 button#click (ByVal sender As Object, ByVal e As System. EventArgs) Handles Button1.ClickUploadThisFile (FileUpload1)
GridView1.DataBind () End Sub

End Class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.