virang_21 0 Light Poster

Hi
I am using ASP.NET to upload files that is to be stored in MSSQL Server 2008 as varbinary(MAX). I have issues with opening scanned pdf files once users upload them. The issue is happening very randomly (say 20% of the files cannot be opened .). It seems something is corrupting those files. My website has 3000 different users who upload files. Following is the code that I use to upload file to SQL Server. Is there any problem in my code ?

if (txtFileContents.PostedFile != null)
                {
                    //Determine File Type
                    string strDocExt = StringUtility.Right(txtFileContents.PostedFile.FileName, 4).ToLower();
                   
                    string strDocType="";
                    switch (strDocExt)
                    {
                        case ".doc":
                            strDocType = "doc";
                            break;
                        case "docx":
                            strDocType = "docx";
                            break;

                        case ".ppt":
                            strDocType = "ppt";
                            break;

                        case ".htm":
                            strDocType = "htm";
                            break;

                        case "html":
                            strDocType = "htm";
                            break;

                        case ".jpg":
                            strDocType = "jpg";
                            break;
                        case "jpeg":
                            strDocType = "jpg";
                            break;

                        case ".gif":
                            strDocType = "gif";
                            break;

                        case ".pdf":
                            strDocType = "pdf";
                            break;
                        case ".png":
                            strDocType = "png";
                            break;

                        default:
                            strDocType = "txt";
                            break;
                    }
                    //Grab the Content of the Uploaded Document
                    try
                    {
                        
                            txtFileContents.PostedFile.SaveAs(Request.PhysicalApplicationPath + "\\" + Session["StudentId"].ToString() + "_" + txtTitle.Text+"."+strDocType);
                            FileInfo fInfo = new FileInfo(Request.PhysicalApplicationPath + "\\" + Session["StudentId"].ToString() + "_" + txtTitle.Text+"."+strDocType);
                            byte[] data = null;
                            FileStream currentFile = new FileStream(Request.PhysicalApplicationPath + "\\" + Session["StudentId"].ToString() + "_" + txtTitle.Text + "."+strDocType, FileMode.Open);
                            BinaryReader breader = new BinaryReader(currentFile);
                            data = breader.ReadBytes((int)fInfo.Length);

                      

                        //int intDocLen = txtFileContents.PostedFile.ContentLength;

                        ////buffer to hold Document Contents
                        //byte[] Docbuffer = new byte[intDocLen];

                        ////InputStream:
                        ////Gets a Stream object which points to an uploaded Document; 
                        ////to prepare for reading the contents of the file.
                        //Stream objStream = txtFileContents.PostedFile.InputStream;

                        ////Store the Content of the Documnet in a buffer
                        ////This buffer will be stored in the Database
                        //objStream.Read(Docbuffer, 0, intDocLen);

                        Student student = new Student();
                        string id = Session["StudentId"].ToString();


                        bool saved = student.UploadStudentDocument(id, txtTitle.Text, strDocType, data);
                        breader.Close();
                        if(saved)
                        {
                            fInfo.Delete();
                        }
                        //objStream.Flush();
                        
                   
                 
                    }
                    catch (Exception ex)
                    {
                        CLogger.WriteLog(ELogLevel.ERROR, ex.Source);
                        CLogger.WriteLog(ELogLevel.ERROR, ex.Message);
                        CLogger.WriteLog(ELogLevel.ERROR, ex.InnerException.ToString());
                        CLogger.WriteLog(ELogLevel.ERROR, ex.StackTrace);
                    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.