asp.net File Upload example__.net

Source: Internet
Author: User
June 14, 2001 14:17:00

asp.net File Upload Example

Jose M. Esteban.
March 28, 2001

Level: beginner/intermediate

Download Source Code

ASP.net provides an easy mechanism to upload files from a Web Browser to a Web Server. The class HtmlInputFile allows to handle programmatically the information submitted by the INPUT control of the type file, e.g., >input type "file". Remember that "Input control" type file is only available in Web Browsers that support HMTL 3.2 and later. This HTML version are supported by IE 3.02 and Netscape 3.0 and their later versions.

There are two objects that you'll use to upload files. One is the "INPUT control" Web Browser and the The other is the HtmlInputFile object in the server. The INPUT control is embedded inside a form that'll post the information encoded in a format suitable for a file tr Ansfer over the Internet. The INPUT control would also load the selected file into the Web Browser ' memory, and'll allow access to the HTMLINPUTFI Le object in the "code that" runs in the "server through" INPUT control ' s ID. i.e., use the ID of the ' INPUT ' control as the ' reference to the ' HtmlInputFile object in the ' code that ' runs at the server .

To upload a file, the form must post the data with the code format Multiform/form-data instead of the default application/ X-www-form-urlencoded that sends the form elements in a Name/value pair base. This restriction arises from the types of formats, that of object of the "HtmlInputFile of", capable and t He characters that can traverse the Internet without confusing the network devices and software. The following example shows how to set the encoding format of a form:

>input type=file id=lofile enctype= "Multipart/form-data"

Once the "data is submitted" to the server, the object of the class HtmlInputFile exposes the property PostedFile, and an O Bject of the type Httppostedfile, that contains the raw file stream and the attributes of the file, such as, file length, File type, file name. But most important, the PostedFile object has the SaveAs method so allows to save the uploaded file to a local directory In the Web server. You can also limit the type of the, the the files you'll allow users to upload by setting the Access property of the Htmlinputfil E object, and you can also discard files base on the ' length by using the ' Postedfile.contentlength property. The following example shows to restrict the upload of video files. The ID lofile is the ID of the INPUT control.

lofile.acess = "video/*"

The example that follows shows you to enable a Web form for file uploads and I to answer the "user with a" message and information about the file that is just uploaded. The Web form is contained in a panel (fileuploadform), which is initially visible and the answer are presented in a panel SWERMSG) is initially disable. Once the "file is uploaded," The Web Form panel is set to invisible and the message panel is set to visible.

The event that handles the "submitted form runs at the" server as indicated by the Submit INPUT tag:

>input type= "Submit" value= "Upload" onserverclick= "uploadfile_clicked" runat= "Server" id=submit1<

The Directory and File classes allow to create a folder to contain the uploaded files and to get the name of the uploaded File filtering the path. In classes we need to include the System.IO name spaces, we can do, including the import directive. This entry allows the "upload" is the name space into the Web Form application, for more information on this topic read the art Icle to Manipulate Files in asp.net:

>%@ Import namespace= "System.IO"%<

For those of this were familiar with uploading files in ASP, the HtmlInputFile class free us from the need of third of Rty components to process the multipart data stream.

Demo This Code

>%@ Import namespace= "System.IO"%<>html< >script language= "VB" runat= "SERVER" id=script1< Sub Uploa Dfile_clicked (Sender as Object, e as EventArgs) Dim lstrfilename As String Dim Lstrfilenamepath As String Dim lstrfilefold Er As String lstrfilefolder = "c:/tempuploadedfiles/" ' Gets the file name lstrfilename = LoFile.PostedFile.FileName LSTRF Ilename = File.getfilenamefrompath (lstrfilename) ' Creates the folder if it does not exists if (not Directory.directoryexi STS (Lstrfilefolder)) Then directory.createdirectories (lstrfilefolder) End If "Save uploaded file to server Lstrfilenamep Ath = lstrfilefolder & lstrfilename loFile.PostedFile.SaveAs (lstrfilenamepath) ' Sets the answer Filename.text = LSTRF	Ilename Filetype.text = LoFile.PostedFile.ContentType Filelength.text = cStr (loFile.PostedFile.ContentLength) Fileuploadform.visible = False Answermsg.visible = True End Sub >/SCRIPT<>BODY< >asp:panel id= FILEUPL Oadform "visible=" true "runat=" serveR "< >form action=" uploadfile.aspx "method=" post "enctype=" Multipart/form-data "runat=" serv Er "id=form1< >h1/h1< Select the file to Upload to" server:>br< >input id= "lofile" type= "file" runat=                "Server" <>br< >input type= "Submit" value= "Upload" onserverclick= "uploadfile_clicked" runat= "Server" id=submit1< >/form< >/ASP:panel< >asp:panel id= "Answermsg" Visi Ble= "false" runat= "Server" < your file >asp:label id= "FileName" runat= "Server"/< has been uploaded >BR< We received >asp:label id= "Filelength" runat= "Server"/< bytes of the type >asp:label id= "File	 Type "runat=" Server/< >/ASP:panel<>/BODY<>/HTML<



trackback:http://tb.blog.csdn.net/trackback.aspx?postid=3472

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.