Example #1
0
        public IHttpActionResult Post(DTO_CUS_DOC_File tbl_CUS_DOC_File)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            DTO_CUS_DOC_File result = BS_CUS_DOC_File.post_CUS_DOC_File(db, PartnerID, tbl_CUS_DOC_File, Username, true);


            if (result != null)
            {
                return(CreatedAtRoute("get_CUS_DOC_File", new { id = result.ID }, result));
            }
            return(Conflict());
        }
Example #2
0
        public IHttpActionResult Post_FileUpload(int id)
        {
            var httpRequest = System.Web.HttpContext.Current.Request;

            if (httpRequest.Files.Count < 1)
            {
                return(BadRequest(ModelState));
            }

            DTO_CUS_DOC_File result = null;
            DTO_CUS_DOC_File item   = new DTO_CUS_DOC_File();

            foreach (string file in httpRequest.Files)
            {
                #region upload file
                var postedFile = httpRequest.Files[file];



                string uploadPath = "/Uploads/DOCs/" + DateTime.Today.ToString("yyyy/MM/dd").Replace("-", "/");


                string oldName = System.IO.Path.GetFileName(postedFile.FileName);
                string ext     = oldName.Substring(oldName.LastIndexOf('.') + 1).ToLower();

                var    g        = Guid.NewGuid();
                string fileid   = g.ToString();
                string fileName = "" + fileid + "." + oldName;
                string viewName = "" + fileid + ".pdf";

                string mapPath          = System.Web.HttpContext.Current.Server.MapPath("~/");
                string strDirectoryPath = mapPath + uploadPath;
                string strFilePath      = strDirectoryPath + "/" + fileName;
                string viewFilePath     = strDirectoryPath + "/" + viewName;

                System.IO.FileInfo existingFile = new System.IO.FileInfo(strFilePath);

                if (!System.IO.Directory.Exists(strDirectoryPath))
                {
                    System.IO.Directory.CreateDirectory(strDirectoryPath);
                }
                if (existingFile.Exists)
                {
                    existingFile.Delete();
                    existingFile = new System.IO.FileInfo(strFilePath);
                }

                postedFile.SaveAs(strFilePath);

                switch (ext)
                {
                case "doc":
                case "docx":
                    convertWordToPDF(strFilePath, viewFilePath);
                    break;

                case "xls":
                case "xlsx":
                    convertExcelToPDF(strFilePath, viewFilePath);
                    break;

                case "ppt":
                case "pptx":
                    convertPowerpointToPDF(strFilePath, viewFilePath);
                    break;

                default:
                    viewName = fileName;
                    break;
                }

                #endregion


                if (id == 0)
                {
                    item.Name      = "";
                    item.IDPartner = 1;
                    item.Code      = uploadPath + "/" + viewName;
                    item.IsDeleted = true;
                    item.Path      = uploadPath + "/" + fileName;
                    item.Extension = System.IO.Path.GetExtension(strFilePath).Replace(".", "");
                    item.FileSize  = new System.IO.FileInfo(strFilePath).Length;

                    result = BS_CUS_DOC_File.post_CUS_DOC_File(db, PartnerID, item, Username, true);
                }
                else
                {
                    item.ID        = id;
                    item.IDPartner = 1;
                    item.Code      = uploadPath + "/" + viewName;
                    item.Path      = uploadPath + "/" + fileName;
                    item.Extension = System.IO.Path.GetExtension(strFilePath).Replace(".", "");
                    item.FileSize  = new System.IO.FileInfo(strFilePath).Length;

                    result = item;


                    //var doc = db.tbl_CUS_DOC_File.Find(id);
                    //if (doc!=null)
                    //{
                    //    doc.Code = item.Code = uploadPath + "/" + viewName;
                    //    doc.Path = item.Path = uploadPath + fileName;
                    //    doc.Extension = item.Extension = System.IO.Path.GetExtension(strFilePath).Replace(".", "");
                    //    doc.FileSize = item.FileSize = new System.IO.FileInfo(strFilePath).Length;
                    //    item.ID = id;
                    //    result = item;
                    //    db.SaveChanges();
                    //}
                    //else
                    //{
                    //    return NotFound();
                    //}
                }
            }


            if (result != null)
            {
                return(CreatedAtRoute("get_CUS_DOC_File", new { id = result.ID }, result));
            }
            return(Conflict());
        }
Example #3
0
        public IHttpActionResult Post_FileUpload(int id)
        {
            var httpRequest = System.Web.HttpContext.Current.Request;

            if (httpRequest.Files.Count < 1)
            {
                return(BadRequest(ModelState));
            }

            DTO_CUS_DOC_File result = null;
            DTO_CUS_DOC_File item   = new DTO_CUS_DOC_File();

            foreach (string file in httpRequest.Files)
            {
                var postedFile = httpRequest.Files[file];

                string uploadPath = "/Uploads/NCKH/" + DateTime.Today.ToString("yyyy/MM/dd").Replace("-", "/");


                string oldName = System.IO.Path.GetFileName(postedFile.FileName);
                string ext     = oldName.Substring(oldName.LastIndexOf('.') + 1).ToLower();

                var    g        = Guid.NewGuid();
                string fileid   = g.ToString();
                string fileName = "" + fileid + "." + oldName;

                string mapPath          = System.Web.HttpContext.Current.Server.MapPath("~/");
                string strDirectoryPath = mapPath + uploadPath;
                string strFilePath      = strDirectoryPath + "/" + fileName;

                System.IO.FileInfo existingFile = new System.IO.FileInfo(strFilePath);

                if (!System.IO.Directory.Exists(strDirectoryPath))
                {
                    System.IO.Directory.CreateDirectory(strDirectoryPath);
                }
                if (existingFile.Exists)
                {
                    existingFile.Delete();
                    existingFile = new System.IO.FileInfo(strFilePath);
                }

                postedFile.SaveAs(strFilePath);


                if (id == 0)
                {
                    item.Name      = oldName;
                    item.Code      = uploadPath + "/" + fileName;
                    item.Path      = uploadPath + "/" + fileName;
                    item.Extension = System.IO.Path.GetExtension(strFilePath).Replace(".", "");
                    item.FileSize  = new System.IO.FileInfo(strFilePath).Length;
                    item.IsHidden  = true;
                    result         = BS_CUS_DOC_File.post_CUS_DOC_File(db, PartnerID, item, Username, true);
                }
                else
                {
                    item.ID        = id;
                    item.Name      = oldName;
                    item.Code      = uploadPath + "/" + fileName;
                    item.Path      = uploadPath + "/" + fileName;
                    item.Extension = System.IO.Path.GetExtension(strFilePath).Replace(".", "");
                    item.FileSize  = new System.IO.FileInfo(strFilePath).Length;
                    item.IsHidden  = true;
                    result         = item;
                }
            }


            if (result != null)
            {
                return(CreatedAtRoute("get_CUS_DOC_File", new { id = result.ID }, result));
            }
            return(Conflict());
        }