Uploading a Filein .net

File Upload has always been a tedious task in ASP. This is due to the encryption type of the form used to submit the file from the clientÂ’s browser. It was a complex task to receive such files on the server side using ASP. Data had to be retrieved as a safe byte array and decrypted before it could be used. Most of the people resorted to 3rd party DLL’s, a few wrote their own DLL’s and some even wrote purely ASP solutions to this problem using VBScript. Fortunately, .NET Framework made uploading files very easy. This can be done with a few lines of code in ASP.NET.

This short tutorial teaches how to add file upload functionality to your ASP.NET page

1. Create an ASP.NET project.

2. Add a web form to the project.

3. Go to the HTML code and change enctype attribute of the form tag to “multipart/form-data”. (We need to do this as we are using HTMLinput control to upload the file from the clients machine)

4. Create an Input box of type FILE.

5. Add a button control to the form and call btnUpload_Click method in the click event.

6. Create a method called btnUpload_Click either in the code behind or in the aspx file and include the following code.

C# code

if (txtUpload.PostedFile !=null)  {        //Get the file name from the client machine       string fileName = txtUpload.PostedFile.FileName.Substring                       (txtUpload.PostedFile.FileName.LastIndexOf("\\") + 1);          //Get the size of the file       int fileSize = txtUpload.PostedFile.ContentLength;        //Get the type of the file       string fileType = txtUpload.PostedFile.ContentType;        if (fileSize 
if not txtUpload.PostedFile =nothing then       'Get the file name from the client machine       Dim fileName as string = txtUpload.PostedFile.FileName.Substring                         (txtUpload.PostedFile.FileName.LastIndexOf("\\") + 1);       'Get the size of the file       Dim fileSize as int = txtUpload.PostedFile.ContentLength       'Get the type of the file       Dim fileType as string = txtUpload.PostedFile.ContentType       if fileSize 
7. The above code is pretty much self-explanatory but do remember to set write permissions on the upload folder.

8. Here is the C# source code for this article.
     void btnUpload_Click(object sender,EventArgs e)     {           if (txtUpload.PostedFile !=null)           {                 string FileName = txtUpload.PostedFile.FileName.Substring                              (txtUpload.PostedFile.FileName.LastIndexOf("\\") + 1); 	        int FileSize = txtUpload.PostedFile.ContentLength; 	        string FileType = txtUpload.PostedFile.ContentType; 	        if (FileSize Upload failed "); 	        else 	        { 	              txtUpload.PostedFile.SaveAs(Server.MapPath(".\\" + FileName)); 	              Response.Write("File uploaded successfully"); 	              Response.Write("
Details"); 	              Response.Write("
File Name: " + FileName); 	              Response.Write("
File Type: " + FileType); 	              Response.Write("
File Size: " + FileSize.ToString()"); 	        }           }     }                         Upload example                                 
File upload
//part twoooooooooooooooooooooooooooooooooooooooooooooooooooo
In Part I, we learned how to upload a single file using ASP.NET. In this part, we are going to learn about how to upload multiple files. Instead of using txtUpload.PostedFile.SaveAs just like we did in the previous article, we loop through the Request.Files collection to save one file at a time.

Here are the steps:

1. Create as many input fields of type “FILE” as you want.
Select files to upload:

2. Create a method called UploadFiles_Click and add the following code.


void UploadFiles_Click(Object sender, EventArgs e) {    String result = "";    String fileName;    HttpPostedFile file;    int i = 0;     //Loop Through the Files    for(i = 0; i = 1)       {          //Get the name of the file		          fileName = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1); 			 	 //Save each uploaded file to the folder where the aspx file exists 	 file.SaveAs(Server.MapPath(".\\" + fileName)); 	 	 result += Server.MapPath(".\\" + fileName) + "Uploaded File: " +               file.FileName + " of type " + file.ContentType + "
";        }    }     if(result == "")    {       result = "Please select atleast one file";    }     //Hide the Panel    PanelUploadMultipleFiles.Visible = false;    Message.Visible = true;    Message.Text = result; }


Sub UploadFiles_Click(ByVal sender as Object, ByVal e as EventArgs)    Dim result as String    Dim filename as String    Dim file as HttpPostedFile    Dim I as Integer = 0     ‘Loop Through the Files    For i = 0 to Request.Files.Count - 1       ‘Get the HttpPostedFile	       file = Request.Files(I)        ‘Check that the File exists has a name and is not empty			       If not (file is nothing or file.FileName = "" or file.ContentLength 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: