Beispiel #1
0
        public ActionResult SaveUploadedFile(long deptid, long prjid, long subtype, long finyr, string docno, string id)
        {
            long     docIdentity = default(long);
            DateTime dt          = DateTime.Now;
            var      doc         = new Document();

            string[] viewerSupportedFiles = ConfigurationManager.AppSettings["ViewerJSSupportedExtenssions"].Split(',');
            try
            {
                if (GlobalSettings.oUserData != null && GlobalSettings.oUserData.ID > default(long))
                {
                    long.TryParse(Encryption64.DecryptQueryString(id), out docIdentity);

                    if (docIdentity <= default(long))
                    {
                        doc.DeptID      = deptid;
                        doc.ProjectID   = prjid;
                        doc.SubTypeID   = subtype;
                        doc.FinYearID   = finyr;
                        doc.DocNumber   = !string.IsNullOrWhiteSpace(docno.Trim()) ? docno.Trim().ToUpper() : "DOC-" + dt.ToString("yyyyMMdd") + "_" + dt.ToString("hhmmss");
                        doc.CreatedBy   = GlobalSettings.oUserData.ID;
                        doc.TaggingType = (int)TaggingType.ScanOnly;
                        doc.DocDate     = dt;
                        doc.Opmode      = 2;
                        var docidstr = DBOperations <Document> .InsertWithOutputVariable(doc, Constant.usp_DocumentManager);

                        long.TryParse(docidstr, out docIdentity);
                    }

                    foreach (string fileName in Request.Files)
                    {
                        try
                        {
                            HttpPostedFileBase file = Request.Files[fileName];
                            var contentLength       = file.ContentLength;
                            if (file != null && contentLength > 0)
                            {
                                byte[] imgAsBytes = new byte[contentLength];
                                using (BinaryReader theReader = new BinaryReader(file.InputStream))
                                {
                                    imgAsBytes = theReader.ReadBytes(contentLength);
                                }
                                //if (!Directory.Exists(Server.MapPath(string.Format("~/Uploads/{0}/", docIdentity))))
                                //{
                                //    Directory.CreateDirectory(Server.MapPath(string.Format("~/Uploads/{0}/", docIdentity)));
                                //}
                                //if (System.IO.File.Exists(Server.MapPath(string.Format("~/Uploads/{0}/{1}", docIdentity, file.FileName))))
                                //{
                                //    System.IO.File.Delete(Server.MapPath(string.Format("~/Uploads/{0}/{1}", docIdentity, file.FileName)));
                                //}
                                //System.IO.File.WriteAllBytes(Server.MapPath(string.Format("~/Uploads/{0}/{1}", docIdentity, file.FileName)), imgAsBytes);
                                if (!Directory.Exists(string.Format(@"{0}\{1}\", ConfigurationManager.AppSettings["DocumentDomainRootDirectory"], docIdentity)))
                                {
                                    Directory.CreateDirectory(string.Format(@"{0}\{1}\", ConfigurationManager.AppSettings["DocumentDomainRootDirectory"], docIdentity));
                                }
                                if (System.IO.File.Exists(string.Format(@"{0}\{1}\{2}", ConfigurationManager.AppSettings["DocumentDomainRootDirectory"], docIdentity, file.FileName)))
                                {
                                    System.IO.File.Delete(string.Format(@"{0}\{1}\{2}", ConfigurationManager.AppSettings["DocumentDomainRootDirectory"], docIdentity, file.FileName));
                                }
                                System.IO.File.WriteAllBytes(string.Format(@"{0}\{1}\{2}", ConfigurationManager.AppSettings["DocumentDomainRootDirectory"], docIdentity, file.FileName), imgAsBytes);

                                AttachmentFile a = new AttachmentFile();
                                a.DocID          = docIdentity;
                                a.FileSizeBytes  = contentLength.ToString();
                                a.FileName       = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
                                a.FileExtenssion = System.IO.Path.GetExtension(file.FileName);
                                //a.FilePath = string.Format("~/Uploads/{0}/{1}", docIdentity, file.FileName); // Set Virtual Path in DB
                                a.FilePath          = string.Format(@"{0}\{1}\{2}", ConfigurationManager.AppSettings["DocumentDomainRootDirectory"], docIdentity, file.FileName); // Set Virtual Path in DB
                                a.IsViewerSupported = viewerSupportedFiles.Contains(System.IO.Path.GetExtension(file.FileName)) ? 1 : default(int);
                                a.CreatedOn         = dt;
                                a.CreatedBy         = GlobalSettings.oUserData.ID;
                                a.Opmode            = 2;
                                var result = DBOperations <AttachmentFile> .DMLOperation(a, Constant.usp_Attachment);
                            }
                        }
                        catch (Exception ex)
                        {
                            //continue;
                            throw ex;
                        }
                    }
                    return(Json(new { success = true, docid = Encryption64.EncryptQueryString(docIdentity.ToString()) }));
                }
                else
                {
                    throw new Exception("Session timed-out. Unable to upload. Please login again");
                }
            }
            catch (Exception)
            {
                return(Json(new { success = false, docid = Encryption64.EncryptQueryString(default(long).ToString()) }));
            }
        }