Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My first file is getting selected only. but in
HttpPostedFile 
second file name is showing.

What I have tried:

protected void btnUploadImage_Click(object sender, EventArgs e)
       {


           string birthDate = Date.Text;
           var split = birthDate.Split('/');
           var yearSplit = split[2];
           string VenName = VendorName.Text;
           //string directoryPath = Server.MapPath(string.Format("~/{0}/", txtFolderName.Text.Trim()));
           // string folderPath =Server.MapPath( @yearSplit + "\\");
           string folderPath = @"E:\\" + yearSplit + "\\";
           string folderPath1 = folderPath + VenName + "\\";
           string folderPath2 = folderPath1 + ddlVoucherType.Text + "\\";
           string file_name = SelectFile.FileName.ToString();
           string folderPathFinal = folderPath2 + file_name;

           //DateTime birthDate1 = DateTime.Now.Date;
           //var year = birthDate1.Year;
           //var month = birthDate1.Month;
           //var day = birthDate1.Day;

           if (SelectFile.HasFiles)
           {
               int filecount = 0;
               int fileuploadcount = 0;
               //check No of Files Selected
               filecount = SelectFile.PostedFiles.Count();
               if (filecount <= 10)
               {

                   foreach (HttpPostedFile postfiles in SelectFile.PostedFiles)
                   {
                       fileuploadcount++;
                       if (ddlVoucherType.Text.ToString() == "Sale")
                       {

                           //Check whether Directory (Folder) exists.

                           if (!Directory.Exists(folderPath) || !Directory.Exists(folderPath1) || !Directory.Exists(folderPath2))
                           {
                               //If Directory (Folder) does not exists. Create it.
                               Directory.CreateDirectory(folderPath2);
                               SelectFile.SaveAs(folderPath2 + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");

                           }

                           else if (!Directory.Exists(folderPath) || !Directory.Exists(folderPath1))
                           {
                               //If Directory (Folder) does not exists. Create it.
                               Directory.CreateDirectory(folderPath1);
                               SelectFile.SaveAs(folderPath1 + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");

                           }
                           else if (!Directory.Exists(folderPath))
                           {

                               Directory.CreateDirectory(folderPath);
                               SelectFile.SaveAs(folderPath + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");

                           }
                           else
                           {
                               SelectFile.SaveAs(folderPath2 + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");

                           }
                       }
                       else
                       {

                           //string folderPath = @"E:\\2016-17\\";
                           //string folderPath1 = @"E:\\2016-17\\" + " Vendor\\";
                           //string folderPath2 = @"E:\\2016-17\\" + " Vendor\\" + "Purchase\\";
                           //Check whether Directory (Folder) exists.

                           if (!Directory.Exists(folderPath) || !Directory.Exists(folderPath1) || !Directory.Exists(folderPath2))
                           {
                               //If Directory (Folder) does not exists. Create it.
                               Directory.CreateDirectory(folderPath2);
                               SelectFile.SaveAs(folderPath2 + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");


                           }

                           else if (!Directory.Exists(folderPath) || !Directory.Exists(folderPath1))
                           {
                               //If Directory (Folder) does not exists. Create it.
                               Directory.CreateDirectory(folderPath1);
                               SelectFile.SaveAs(folderPath1 + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");


                           }
                           else if (!Directory.Exists(folderPath))
                           {

                               Directory.CreateDirectory(folderPath);
                               SelectFile.SaveAs(folderPath + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");

                           }
                           else
                           {
                               SelectFile.SaveAs(folderPath2 + Path.GetFileName(SelectFile.FileName));
                               MessageBox.Show(Path.GetFileName(SelectFile.FileName) + " has been uploaded.");

                           }

                       }
                     //  fileuploadcount++;
                   }
               }

               Stream fs = SelectFile.PostedFile.InputStream;
               BinaryReader br = new BinaryReader(fs);
               Byte[] bytes = br.ReadBytes((Int32)fs.Length);




               //save data
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

               SqlCommand cmd = new SqlCommand("SaveUploadFiles", con);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("ImageName", file_name);
               cmd.Parameters.AddWithValue("ImagePath", folderPathFinal);
               cmd.Parameters.AddWithValue("Image", fs);
               cmd.Parameters.AddWithValue("VoucherType", ddlVoucherType.Text);
               cmd.Parameters.AddWithValue("VendorId", VenName);
               cmd.Parameters.AddWithValue("VoucherYear", birthDate);
               //cmd.Parameters.AddWithValue("UpdatedBy", VenName);
               con.Open();
               int k = cmd.ExecuteNonQuery();
               if (k != 0)
               {
                   MessageBox.Show("Record Inserted Succesfully into the Database");
                   //lblmsg.ForeColor = System.Drawing.Color.CornflowerBlue;
                   VendorName.Text = string.Empty;
                   ddlVoucherType.SelectedIndex = -1;
                   Date.Text = string.Empty;


               }
               con.Close();
           }
       }
Posted
Updated 11-Mar-17 4:48am
Comments
Karthik_Mahalingam 8-Mar-17 1:04am    
what is the count
filecount = SelectFile.PostedFiles.Count();
Rakesh R Surve[RST] 8-Mar-17 2:36am    
filecount = string,
selectFile= file upload control name,
cand count gives me 5 when i select 5 files to upload
Karthik_Mahalingam 8-Mar-17 2:39am    
MessageBox.Show in web app ?
Rakesh R Surve[RST] 8-Mar-17 3:38am    
Not getting Sir...
Karthik_Mahalingam 8-Mar-17 3:48am    
is this windows app or web ?

1 solution

kill me, a new invention is here an his name is WEB or www !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900