How to use File Upload Control in ASP.Net

How to use FILE Upload Control in ASP.Net Page  ???

File Upload Control has given a great flexibility to Web Programmers by allowing to show the Disk Contents of Client Machine and also makes it available to be saved on Server, so that may be retrievece and used later on as and when required.

We will be understanding the concepts of this control with the help of example explained below:

On your Page, Draw a FILE UPLOAD Control with a name "FileUpload1" and a Button having the Text  "Upload" 

When the "Upload" button is clicked, you need o write following code which is meant to upload  only  JPG format image file

    dim str as String

    if    FileUpload1.hasFile  Then                                                                                (1)
           
           if  FileUpload1.PostedFile.ContentType= "image/pjpeg"  Then                 (2)

                 if  FileUpload1.PostedFile.ContentLength  < 102400  Then                  (3)

                      str =FileUpload1.FileName                                                                (4)

                      FileUpload1.SaveAs(server.mappath("~/")  + str)                          (5)

                      msgbox ("Upload Successfull")
                 
                 else

                           msgbox  "File too Large to Upload"
                 end if
            
            else
             
                  msgbox  "Only JPEG Accepted"
   
             end if

    else

            msgbox  "Upload Failed, No File Selected to Upload"

    end if

(1)       FileUpload1.hasFile  This method returns true  if any file has been selected for uploading

(2)       FileUpload1.PostedFile.ContentType  This method returns the type of selected File so that we
            may compare it with the type that we want to upload 

(3)       FileUpload1.PostedFile.ContentLength   This method checks the length of selected file

(4)       FileUpload1.FileName   This method returns the name of selected File Name

(5)       FileUpload1.SaveAs(server.mappath("~/")  + str) This method save the selected file at SERVER

Note : This code has been given only for JPEG format file , you may change the type of file you want 

Comments

Popular posts from this blog

Understanding Working of Text & Binary File in C

List of C Programs that makes u understand concepts in an easy fashion(Part1)

Starting C : A series of Chapters to learn C