Example #1
0
        /// <summary>
        /// Get: 新增附件資訊
        /// </summary>
        /// <param name="id">機種ID</param>
        /// <returns></returns>
        public ActionResult Create(int id)
        {
            UploadFile upload = new UploadFile();

            var query = (from module in db.RDRModules.Where(m => m.ID == id)
                        join main in db.RDRMains on module.ParentID equals main.ID
                        select new
                        {
                            MainID = main.ID,
                            ModuleID = module.ID,
                        }).FirstOrDefault();

            if (query != null)
            {
                upload.MainID = query.MainID;
                upload.ModuleID = query.ModuleID;
                upload.UploadType = 1; //RFQ

                return View(upload);
            }

            return View(upload);
        }
Example #2
0
        public ActionResult Create(UploadFile upload, HttpPostedFileBase file)
        {
            string fileName = "";
            string path = "";
            bool IsUploadOK = false;

            //上傳檔案驗證
            if (file != null)
            {
                #region 上傳
                //從RDRNumber 取出資料夾結構: RFQFiles > 客戶群> ProjectName > 交貨地 > ModuleName > Version
                string UploadFilesType = "RFQFiles";
                string MainCode = db.RDRMains.Where(m => m.ID == upload.MainID).Select(m => m.RDRNumber).FirstOrDefault();
                var query = (from m in db.RDRModules.Where(m => m.ID == upload.ModuleID)
                                       join c in db.Customers on m.CustomerID equals c.ID
                                       select new { ModuleName = m.ModuleName, customerName = c.Name }).FirstOrDefault();

                string team = MainCode.Substring(0, MainCode.IndexOf('.')); //取出客戶群,ex. AL.15.0001
                string projName = db.RDRMains.Where(m => m.ID == upload.MainID).Select(m => m.ProjectName).FirstOrDefault();  //專案名稱
                string version = ".0"; //新增RDR表單的機種內容,預設版本號為0

                //UP是IIS的虛擬目錄,於IIS設定指向Web Server上的實體資料夾
                var target = "/UP/" + UploadFilesType + "/" + team + "/" + query.customerName + "/" + projName + "/" + query.ModuleName + "/" + version;
                path = Server.MapPath(target);

                bool IsUpload = GOUpload(file); //驗證是否真的有檔案上傳 & 驗證檔案大小、格式是否符合規範
                if (IsUpload)
                {
                    fileName = System.IO.Path.GetFileName(file.FileName); //完整檔名

                    try
                    {
                        if (!System.IO.Directory.Exists(path))
                        {
                            System.IO.Directory.CreateDirectory(path); //檢查資料夾路徑是否存在, 不存在就自動建立
                        }

                        path = System.IO.Path.Combine(path, fileName);
                        file.SaveAs(path); //檔案存放到儲存路徑上

                        IsUploadOK = true;
                    }
                    catch (Exception ex)
                    {
                        path = "";
                        return ShowMsgThenRedirect(ex.ToString(), Url.Action("List", "UploadFiles", new { id = upload.ModuleID }));
                    }
                }
                else
                {
                    path = "";
                }
                #endregion
            }

            if (IsUploadOK)
            {
                //新增附件資訊到 db.UploadFile
                upload.Name = fileName;
                upload.Location = path;
                upload.ContentType = file.ContentType;
                upload.FileSize = file.ContentLength;
                upload.CreateDateTime = DateTime.Now;
                upload.ModifyDateTime = DateTime.Now;
                upload.CreateUserID = 1;
                upload.ModifyUserID = 1;

                try
                {
                    db.UploadFiles.Add(upload);
                    db.SaveChanges();

                    return ShowMsgThenRedirect("新增成功", Url.Action("List", "UploadFiles", new { id = upload.ModuleID }));
                }
                catch (Exception ex)
                {
                    //新增記錄失敗時,要刪除附件,讓資訊一致
                    //刪除已上傳的的檔案
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                    return ShowMsgThenRedirect(ex.ToString(), Url.Action("List", "UploadFiles", new { id = upload.ModuleID }));
                }
            }
            return ShowMsgThenRedirect("新增失敗", Url.Action("List", "UploadFiles", new { id = upload.ModuleID }));
        }
Example #3
0
        public ActionResult Create(RDRModuleCreateViewModel viewModel, HttpPostedFileBase file, FormCollection fc)
        {
            string fileName = "";
            string path = "";
            bool IsUploadOK = false;
            bool IsWriteToRDRModule = false;
            int moduleID = 0;

            int pid = 0;
            int.TryParse(fc["ProductGroupsList"], out pid); //表單產品別ID

            int cid = 0;
            int.TryParse(fc["CustomersList"], out cid); //表單客戶ID

            //1.若有上傳附件則執行上傳驗證,若無上傳略過此步驟
            if (file != null)
            {
                #region 上傳
                //從RDRNumber 取出資料夾結構: RFQFiles > 客戶群> ProjectName > 交貨地 > ModuleName > Version
                string UploadFilesType = "RFQFiles";
                string team = viewModel.MainCode.Substring(0, viewModel.MainCode.IndexOf('.')); //取出客戶群,ex. AL.15.0001
                string projName = db.RDRMains.Find(viewModel.ParentID).ProjectName;      //專案名稱
                string customerName = db.Customers.Find(cid).Name;
                string version = ".0"; //新增RDR表單的機種內容,預設版本號為0

                //UP是IIS的虛擬目錄,於IIS設定指向Web Server上的實體資料夾
                var target = "/UP/" + UploadFilesType + "/" + team + "/" + customerName + "/" + projName + "/" + viewModel.ModuleName + "/" + version;
                path = Server.MapPath(target);

                bool IsUpload = GOUpload(file); //驗證是否真的有檔案上傳 & 驗證檔案大小、格式是否符合規範
                if (IsUpload)
                {
                    fileName = System.IO.Path.GetFileName(file.FileName); //完整檔名

                    try
                    {
                        if (!System.IO.Directory.Exists(path))
                        {
                            System.IO.Directory.CreateDirectory(path); //檢查資料夾路徑是否存在, 不存在就自動建立
                        }

                        path = System.IO.Path.Combine(path, fileName);
                        file.SaveAs(path); //檔案存放到儲存路徑上

                        IsUploadOK = true;
                    }
                    catch (Exception ex)
                    {
                        path = "";
                        return ShowMsgThenRedirect(ex.ToString(), Url.Action("Create", "RDRModule", new { id = viewModel.ParentID }));
                    }
                }
                else
                {
                    path = "";
                }
                #endregion
            }

            //2.上傳驗證OK>>寫入db.RDRModule,若寫入db.RDRModule失敗則刪除附件&拋出例外
            try
            {
                RDRModule rdrModule = new RDRModule();
                rdrModule.ParentID = viewModel.ParentID;
                rdrModule.RDRNumber = CreateRDRNumber(viewModel.MainCode, pid, cid);
                rdrModule.ModuleName = viewModel.ModuleName;

                if (string.IsNullOrWhiteSpace(viewModel.CustomerBOM))
                {
                    rdrModule.CustomerBOM = "N/A";  //客戶料號
                }
                else {
                    rdrModule.CustomerBOM = viewModel.CustomerBOM;
                }

                rdrModule.ProductGroupID = pid;                     //產品別ID
                rdrModule.CustomerID = cid;                         //交貨地ID
                rdrModule.EstimateProduct = viewModel.EstimateProduct;
                rdrModule.Attachment = path;
                rdrModule.Remark = viewModel.Remark;
                rdrModule.ModuleVersion = 0;    //版本號 預設值0
                rdrModule.IsDelete = false;
                rdrModule.CreateTime = DateTime.Now;

                db.RDRModules.Add(rdrModule);
                db.SaveChanges();

                IsWriteToRDRModule = true;
                moduleID = rdrModule.ID;
            }
            catch(Exception ex)
            {
                //刪除已上傳的的檔案
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
                return ShowMsgThenRedirect(ex.ToString(), Url.Action("Create", "RDRModule", new { id = viewModel.ParentID }));
            }

            //3.寫入db.RDRModule OK>>將附件資訊寫入db.UploadFile,若寫入db.UploadFile失敗則 delete db.RDRModule 與附件, 保持 RDRModule 與 UploadFile 資料一致
            if (IsUploadOK && IsWriteToRDRModule)
            {
                try
                {
                    UploadFile uploadfile = new UploadFile();
                    uploadfile.ModuleID = moduleID;  // UploadFile.RefID 參考 RDRModule.ID
                    uploadfile.Name = fileName;
                    uploadfile.FileSize = file.ContentLength;
                    uploadfile.UploadType = 1;
                    uploadfile.ContentType = file.ContentType;
                    uploadfile.Location = path;
                    uploadfile.IsProductSpec = viewModel.IsProductSpec;
                    uploadfile.IsTestInstruction = viewModel.IsTestInstruction;
                    uploadfile.IsCustomerBOM = viewModel.IsCustomerBOM;
                    uploadfile.IsBinResistorTable = viewModel.IsBinResistorTable;
                    uploadfile.IsPCBA = viewModel.IsPCBA;
                    uploadfile.IsPCB = viewModel.IsPCB;
                    uploadfile.IsHarness = viewModel.IsHarness;
                    uploadfile.IsGerber = viewModel.IsGerber;
                    uploadfile.IsCoordinate = viewModel.IsCoordinate;
                    uploadfile.IsSchematics = viewModel.IsSchematics;
                    uploadfile.IsComp = viewModel.IsComp;
                    uploadfile.IsPVTestPlan = viewModel.IsPVTestPlan;
                    uploadfile.IsSVRF = viewModel.IsSVRF;
                    uploadfile.CreateDateTime = DateTime.Now;
                    uploadfile.CreateUserID = 1;
                    uploadfile.ModifyDateTime = DateTime.Now;
                    uploadfile.ModifyUserID = 1;

                    db.UploadFiles.Add(uploadfile);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    //刪除附件
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }

                    //刪除RDRModule記錄
                    RDRModule mo = db.RDRModules.Where(m => m.ID == moduleID).FirstOrDefault();
                    if (mo != null)
                    {
                        db.RDRModules.Remove(mo);
                        db.SaveChanges();
                    }

                    return ShowMsgThenRedirect(ex.ToString(), Url.Action("Index", "RDRManage"));
                }
            }

            return RedirectToAction("Create", new { id = viewModel.ParentID });
        }