C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Multiple File Upload


Posted Date: 14 Dec 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: DharmarajMember Level: Diamond    
Rating: 1 out of 5Points: 10



This code will explain how to perform multiple file upload with asp.net
Create a Web application project. Place a label,textbox,two button and a panel in the webform. Change the caption of the label to enter the number of files to be uploaded. Change the textbox id property to txtcount and the id property to pnlupload. Change the text property of the button to Generate and the id property to btngenerate of first button. Change the text property of second button to Upload, id property to btnupload.

In the click event of the Generate button write the following code.

protected void btngenerate_Click(object sender, EventArgs e)
{
//if the count is not empty then generate
//appropriate number of file upload control
if (txtfiles.Text != "")
{
int count = int.Parse(txtfiles.Text);
for (int i = 0; i < count; i++)
{
FileUpload fu = new FileUpload();
pnlupload.Controls.Add(fu);
}
}
else
{
//Respond with alert message
Response.Write("");
}
}

Add a label to show the status of the file name,file status of the file being uploaded. In the click event of the upload button write the following code

protected void Button1_Click(object sender, EventArgs e)
{
string filepath = "C:\\Uploads";
HttpFileCollection uploadedFiles = Request.Files;

for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];

try
{
if (userPostedFile.ContentLength > 0 )
{
Label1.Text += "File #" + (i+1) +
"

";
Label1.Text += "File Content Type: " +
userPostedFile.ContentType + "
";
Label1.Text += "File Size: " +
userPostedFile.ContentLength + "kb
";
Label1.Text += "File Name: " +
userPostedFile.FileName + "
";

userPostedFile.SaveAs(filepath + "\\" +
System.IO.Path.GetFileName(userPostedFile.FileName));

Label1.Text += "Location where saved: " +
filepath + "\\" +
System.IO.Path.GetFileName(userPostedFile.FileName) +
"

";
}
}
catch (Exception Ex)
{
Label1.Text += "Error:
" + Ex.Message;
}
}
}


Regards,
Dharma



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Multiupload with ASP.NET  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Popup window in ASP.NET
Previous Resource: Get IP Address
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use