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
|
No responses found. Be the first to respond and make money from revenue sharing program.
|