I think this is the better way to do it. Not sure why your code is displaying 0x000000001 in the hfc.GetKey(i) function, you need to investigate that.
public class FileUploadList
{
public int Position { get; set; }
public string ControlId { get; set; }
public string filePath { get; set; }
}
string path1, path2, path3, path4, path5, path6, path7, path8, path9, path10, path11, path12, path13, path14, path15,path16, path17, path18, path19, path20, path21, path22, path23, path24, path25 = null;
internal void FileUploadHelper(HttpFileCollection hfc, List<FileUploadList> lstFile)
{
if (lstFile.Count > 0 )
{
string fileExtension = string.Empty;
int fileSize = 0;
int arrPos = 0;
string[] arr = new string[5];
foreach (FileUploadList file in lstFile)
{
string filePath = file.filePath;
fileExtension = System.IO.Path.GetExtension(hfc[file.Position].FileName);
fileSize = hfc[file.Position].ContentLength;
if (hfc[file.Position].ContentLength > 0)
{
hfc[file.Position].SaveAs(Server.MapPath(string.Format("~/{0}/", filePath)) + System.IO.Path.GetFileName(hfc[file.Position].FileName));
string filepath = Server.MapPath(string.Format("~/{0}/", filePath));
string path = string.Format("{0}/{1}", filePath, System.IO.Path.GetFileName(hfc[file.Position].FileName));
arr[arrPos] = path;
arrPos++;
}
}
switch (lstFile.FirstOrDefault().ControlId)
{
case "flpProspectus":
path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];
break;
case "flpOrientation":
path6 = arr[0]; path7 = arr[1]; path8 = arr[2]; path9 = arr[3]; path10 = arr[4];
break;
case "flpEnquiry":
path11 = arr[0]; path12 = arr[1]; path13 = arr[2]; path14 = arr[3]; path15 = arr[4];
break;
case "flpPunishment":
path16 = arr[0]; path17 = arr[1]; path18 = arr[2]; path19 = arr[3]; path20 = arr[4];
break;
case "flpnooffir":
path21 = arr[0]; path22 = arr[1]; path23 = arr[2]; path24 = arr[3]; path25 = arr[4];
break;
}
}
}
public void FileUploads()
{
if (flpProspectus.HasFile || flpOrientation.HasFile || flpEnquiry.HasFile || flpPunishment.HasFile || flpnooffir.HasFile)
{
HttpFileCollection hfc = Request.Files;
List<FileUploadList> lstFile = new List<FileUploadList>();
string filePath = string.Empty;
for (int i = 0; i < hfc.Count; i++)
{
FileUploadList ful = new FileUploadList();
if (hfc[i].ContentLength > 0)
{
if (hfc.GetKey(i).Contains(flpOrientation.ID))
{
ful.Position = i;
ful.ControlId = flpOrientation.ID;
ful.filePath = "college/fileupload3";
}
if (hfc.GetKey(i).Contains(flpProspectus.ID))
{
ful.Position = i;
ful.ControlId = flpProspectus.ID;
ful.filePath = "college/fileupload4";
}
if (hfc.GetKey(i).Contains(flpEnquiry.ID))
{
ful.Position = i;
ful.ControlId = flpEnquiry.ID;
ful.filePath = "college/fileupload5";
}
if (hfc.GetKey(i).Contains(flpPunishment.ID))
{
ful.Position = i;
ful.ControlId = flpPunishment.ID;
ful.filePath = "college/fileupload6";
}
if (hfc.GetKey(i).Contains(flpnooffir.ID))
{
ful.Position = i;
ful.ControlId = flpnooffir.ID;
ful.filePath = "college/fileupload7";
}
lstFile.Add(ful);
}
}
FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpOrientation.ID).ToList());
FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpProspectus.ID).ToList());
FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpEnquiry.ID).ToList());
FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpPunishment.ID).ToList());
FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpnooffir.ID).ToList());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
FileUploads();
}