Greetings again gurus,
I have a markup that allows me to upload multiple files.
Users can upload up to 5 files at once.
Initially, I use the first C# code below to upload just one file.
Now, I am trying to use the second C# code at the bottom to capture the number of files uploaded.
My questions are:
1, how do I change the markup to show something like:
Browse__________________________ Add more files.
Users will browse and add a file, click the Add button to give another browse____ to upload second file.
Do that for up to 5?
Second, how do I integrate the first C# code with the second?
I am more interested in integrating the FOR loop of the second C# code with the first C# code.
I hope this is not confusing. This environment didn't allow me to post the code seperately with comments in between.
Thanks as always for your assistance
//Markup
<asp:FileUpload ID="FileUploadToServer" Width="300px" AllowMultiple="true" runat="server" />
C#
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
bool blSucces = false;
string filename = string.Empty;
if (FileUploadToServer.HasFile)
{
try
{
string[] allowdFile = { ".pdf,.png,.jpg,.jpeg,.gif,.docx,.doc,.xls,.xlsx" };
string FileExt = System.IO.Path.GetExtension(FileUploadToServer.PostedFile.FileName);
bool isValidFile = allowdFile.Contains(FileExt);
if (!isValidFile)
{
lblResult.ForeColor = System.Drawing.Color.Red;
lblResult.Text = "Please upload onlythe following: pdf,png,jpg,jpeg,gif,docx,doc,xls,xlsx";
}
else
{
if (File.Exists(Server.MapPath(FilePath) + filename))
{
File.Delete(Server.MapPath(FilePath) + filename);
}
int FileSize = FileUploadToServer.PostedFile.ContentLength;
if (FileSize <= 104857600) // up to 100mb allowed
{
filename = Path.GetFileName(FileUploadToServer.FileName);
FileUploadToServer.SaveAs(Server.MapPath(FilePath) + Session["empnum"] + filename);
lblResult.Text = "File uploaded successfully!";
lblResult.ForeColor = System.Drawing.Color.Red;
blSucces = true;
}
else
{
lblResult.Text = "Attachment file size should not be greater then 250 MB";
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
}
catch (Exception ex)
{
lblResult.Text = "Error occurred while uploading a file: " + ex.Message;
lblResult.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblResult.Text = "Please select a file to upload.";
lblResult.ForeColor = System.Drawing.Color.Red;
}
if (blSucces)
{
Updatefileinfo(filename, FilePath + filename);
}
}
C#
protected void btnSubmit_Click(object sender,EventArgs e) {
string filepath = Server.MapPath("\\Upload");
HttpFileCollection uploadedFiles = Request.Files;
Span1.Text = string.Empty;
for(int i = 0;i < uploadedFiles.Count;i++) {
HttpPostedFile userPostedFile = uploadedFiles[i];
try {
if (userPostedFile.ContentLength > 0) {
Span1.Text += "<u>File #" + (i + 1) + "</u><br>";
Span1.Text += "File Content Type: " + userPostedFile.ContentType + "<br>";
Span1.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>";
Span1.Text += "File Name: " + userPostedFile.FileName + "<br>";
userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName));
Span1.Text += "Location where saved: " + filepath + "\\" + Path.GetFileName(userPostedFile.FileName) + "<p>";
}
} catch(Exception Ex) {
Span1.Text += "Error: <br>" + Ex.Message;
}
}
}
}