I had implemented FileUpload code. In which for every particular Id, it creates Folder/Directory (if not exist) on Page _load.
Then inside that particular Id's folder it will upload single or multiple files.
HTML:
<asp:Label ID="LFileUpload" runat="server" Text="Upload File"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" Width="300px" AllowMultiple="true"/>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" ValidationGroup="none"/>
<asp:Label ID="LMsg" runat="server" Text="" Visible="false"></asp:Label>
C#:
//code for "Upload" button
protected void UploadFile(object sender, EventArgs e)
{
string filePath = DamRep_FileUpload.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
case ".txt":
contenttype = "application/txt";
break;
}
if (contenttype != String.Empty)
{
if (File.Exists(Server.MapPath("~/FTP/CMMS/DamRep/'" + TID.Text + "'/") + filename))
{
LMsg.Visible = true;
LMsg.ForeColor = Color.Red;
LMsg.Text = "File name already exist. Change the File name and then upload if u want to save this file";
}
else
{
//saving file in newly created folder
string fileName = Path.GetFileName(FileUpload1.PostedFiles.FileName);
FileUpload1.PostedFiles.SaveAs(Server.MapPath("~/FTP/CMMS/DamRep/'" + TID.Text + "'/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
else
{
LMsg.Visible = true;
LMsg.ForeColor = System.Drawing.Color.Red;
LMsg.Text = "File format not recognised." + " Upload Image/Word/PDF/Excel formats";
}
}
Problem is: When I click on "Browse" button, window opens, user can select single or multiple files to upload.
and when user clicks on "Upload" button to upload selected files, then only 1st selected file is uploaded not all.
I tried below code:
foreach (var file in FileUpload1.PostedFiles)
{
//saving file in newly created folder
string fileName = Path.GetFileName(file.PostedFile.FileName);
file.PostedFile.SaveAs(Server.MapPath("~/FTP/CMMS/DamRep/'" + TID.Text + "'/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
But not working.
Please reply how to solve this problem.