Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
if (gvAttachments.Rows.Count > 0)
          {
              string[] Records = Session["FileDetail"].ToString().Split('#');
              for (int i = 0; i < Records.Length; i++)
              {
                  string[] arrRecords = Records[i].Split(',');
                  string[] fileExtension = arrRecords[0].Split('.');
                  string documentType = string.Empty;
                  switch (fileExtension[1])
                  {
                      case "pdf": documentType = "application/pdf";
                          break;
                      case "xls": documentType = "application/vnd.ms-excel";
                          break;
                      case "xlsx": documentType = "application/vnd.ms-excel";
                          break;
                      case "doc": documentType = "application/vnd.ms-word";
                          break;
                      case "docx": documentType = "application/vnd.ms-word";
                          break;
                      case "gif": documentType = "image/gif";
                          break;
                      case "png": documentType = "image/png";
                          break;
                      case "jpg": documentType = "image/jpg";
                          break;
                  }
                  string size = arrRecords[2];
                  int fileSize = Convert.ToInt32(size);
                  byte[] documentbinary = new byte[fileSize];
                  fuUpload.PostedFile.InputStream.Read(documentbinary, 0, fileSize);
                  string file = arrRecords[0].ToString();
                  string format = documentType.ToString();
                  cmd = new SqlCommand();
                  cmd.CommandText = "Insert into Attachment(File_Name,File_Format,File_Content,User_Notice_Mapping_ID) values('" + file + "','" + format + "',@DocData," + userNoticeID + ")";
                  cmd.CommandType = CommandType.Text;
                  cmd.Connection = con;
                  cmd.Parameters.Add("@DocData", SqlDbType.VarBinary, fileSize);
                  cmd.Parameters["@DocData"].Value = documentbinary;
                  cmd.ExecuteNonQuery();
                  String msg = "Your message has been Sent Successfully";
                  ShowAlert(msg, true);
              }
          }
Posted
Comments
Herman<T>.Instance 26-Apr-14 2:09am    
string size = arrRecords[2];
int fileSize = Convert.ToInt32(size);

is arrREcords[2] filled ?

1 solution

You should replace the next line
int fileSize = Convert.ToInt32(size);

with the next one:
int fileSize = (int)fuUpload.PostedFile.InputStream.Length;
 
Share this answer
 
Comments
Neha Mukesh 28-Apr-14 5:29am    
i am uploading multiple files so my fuupload doesnot contain any file ,i have stored all the files in the # separated session ...that's why i can't write fuupload.postedfile.Contentlength ...now how to get correct file size

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