Esempio n. 1
0
        private void insertDocument(Model.Entities.Document document, SqlConnection condocument)
        {
            string     strInsert = "Insert Into Document (DocumentName, DocumentType, FileIDInCloud, CustomerID, Size, PageNumber) Values (@DocumentName, @DocumentType, @FileIDInCloud, @CustomerID, @Size, @PageNumber);SELECT MAX(DocumentID) from Document where DocumentName=@DocumentName and DocumentType=@DocumentType";
            SqlCommand cmdInsert = new SqlCommand(strInsert, condocument);

            cmdInsert.Parameters.AddWithValue("@DocumentName", document.DocumentName);
            cmdInsert.Parameters.AddWithValue("@DocumentType", document.DocumentType);
            cmdInsert.Parameters.AddWithValue("@FileIDInCloud", document.FileIDInCloud);
            cmdInsert.Parameters.AddWithValue("@CustomerID", document.CustomerID);
            cmdInsert.Parameters.AddWithValue("@Size", document.Size);
            cmdInsert.Parameters.AddWithValue("@PageNumber", document.PageNumber);

            var getDocumentID = cmdInsert.ExecuteScalar();

            document.DocumentID = getDocumentID.ToString();
        }
        private List <Documentlist> createDocumentList()
        {
            string userID = ClassHashing.basicDecryption((string)ViewState["UserID"]);

            //create document list and upload file to cloud
            List <Documentlist> documentList = new List <Documentlist>();
            int totalpage   = 0;
            int count       = 0;
            int normalCount = 0;

            if (FileUpload1.HasFile)

            {
                HttpFileCollection hfc = Request.Files;
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        //upload to backblaze
                        String contentType = hpf.ContentType;                  //Type of file i.e. image/jpeg, audio/mpeg...
                        String getPath     = Path.GetFileName(hpf.FileName);
                        hpf.SaveAs(Server.MapPath("~/File/") + getPath);       //save to server side file
                        String fileName = hpf.FileName;                        //Desired name for the file
                        String filePath = Server.MapPath("~/File/") + getPath; //File path of desired upload
                        int    size     = FileUpload1.PostedFile.ContentLength;


                        lblgetallfilename.Text += String.Format("{0} , ", fileName);

                        string getallfilename = lblgetallfilename.Text;
                        Session["allfilename"] = getallfilename;

                        Session["pathfile"] = filePath;

                        string getFileIDInCloud = backblaze.UploadFile(contentType, filePath, fileName);
                        int    numberOfPages    = 0;
                        if (Path.GetExtension(hpf.FileName).Equals(".docx"))
                        {
                            // get the page number
                            var application = new Application();
                            var document    = application.Documents.Open(filePath); //open document
                            numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false);
                            document.Close();                                       ///close document

                            //get the count of file
                            count = hfc.Count;
                            Session["countthepackageitem"] = count;

                            FileInfo file = new FileInfo(filePath);
                            if (file.Exists)//check file exsit or not
                            {
                                file.Delete();
                            }
                        }
                        else if (Path.GetExtension(hpf.FileName).Equals(".pdf"))
                        {
                            FileStream      fs      = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                            StreamReader    r       = new StreamReader(fs);
                            string          pdfText = r.ReadToEnd();
                            Regex           rx1     = new Regex(@"/Type\s*/Page[^s]");
                            MatchCollection matches = rx1.Matches(pdfText);
                            numberOfPages = Convert.ToInt32(matches.Count.ToString());
                            fs.Close();

                            //get the count of file
                            count = hfc.Count;
                            Session["countthepackageitem"] = count;

                            FileInfo file = new FileInfo(filePath);
                            if (file.Exists)//check file exsit or not
                            {
                                file.Delete();
                            }
                        }
                        else if (Path.GetExtension(hpf.FileName).Equals(".png") || Path.GetExtension(hpf.FileName).Equals(".PNG") || Path.GetExtension(hpf.FileName).Equals(".jpg"))
                        {
                            count = hfc.Count - 1;
                            Session["countthepackageitem"] = count;
                            numberOfPages = 1;

                            FileInfo file = new FileInfo(filePath);
                            if (file.Exists)//check file exsit or not
                            {
                                file.Delete();
                            }
                        }


                        //calculate the total page in multiple file

                        totalpage           += numberOfPages;
                        Session["totalpage"] = totalpage;

                        normalCount            = hfc.Count;
                        Session["normalcount"] = normalCount;


                        // upload to my sqldatabase
                        var    uploadFileObject = (JObject)JsonConvert.DeserializeObject(getFileIDInCloud);
                        String FileIdInCloud    = uploadFileObject["fileId"].Value <string>();//get file ID



                        Model.Entities.Document newdocument = new Model.Entities.Document(fileName, contentType, FileIdInCloud, userID, size, numberOfPages);

                        int    sequences           = 0; ////remember to do it;
                        string documentColor       = "null";
                        string documentbothside    = rbtDocumentSide.SelectedValue;
                        int    documentpapertype   = Convert.ToInt32(ddlPaperType.SelectedValue);
                        int    documentquantity    = Convert.ToInt32(txtDocumentQuantity.Text);
                        string documentdescription = txtDocumentDescription.Text;

                        documentList.Add(new Documentlist(newdocument, sequences, documentColor, documentbothside, documentpapertype, documentquantity, documentdescription));
                    }
                }
            }

            return(documentList);
        }