diff --git a/AMESCoreStudio.Web/Controllers/LABController.cs b/AMESCoreStudio.Web/Controllers/LABController.cs index aaec3031..1a24770b 100644 --- a/AMESCoreStudio.Web/Controllers/LABController.cs +++ b/AMESCoreStudio.Web/Controllers/LABController.cs @@ -17,6 +17,8 @@ using Microsoft.AspNetCore.Mvc.Rendering; using AMESCoreStudio.WebApi.DTO.AMES; using AMESCoreStudio.Web.ViewModels.LAB; using System.Dynamic; +using System.IO; +using Microsoft.AspNetCore.Hosting; namespace AMESCoreStudio.Web.Controllers { @@ -25,15 +27,16 @@ namespace AMESCoreStudio.Web.Controllers public readonly ISYS _sysApi; public readonly ILAB _labApi; public readonly IPCS _pcsApi; + private readonly IWebHostEnvironment _env; - public LABController(ILogger logger, ILAB labApi, ISYS sysApi, IPCS pcsApi, IStringLocalizer sharedLocalizer) + public LABController(ILogger logger, ILAB labApi, ISYS sysApi, IPCS pcsApi, IWebHostEnvironment env, IStringLocalizer sharedLocalizer) { _sysApi = sysApi; _labApi = labApi; _pcsApi = pcsApi; - + _env = env; } private void GetFlagList() @@ -246,12 +249,45 @@ namespace AMESCoreStudio.Web.Controllers //頁面提交,id=0 添加,id>0 修改 [HttpPost] - public async Task LAB002CSaveAsync(LabelTemplateMaster model) + public async Task LAB002CSaveAsync(LabelTemplateMaster model, IFormFile formFile) { if (ModelState.IsValid) { IResultModel result; + string FileName = string.Empty; + string NewName = string.Empty; + string FilePath = string.Empty; + + if (formFile != null) + { + if (formFile.Length > 0) + { + //取得使用者上傳檔案的原始檔名 + FileName = Path.GetFileName(formFile.FileName); + //取原始檔名中的副檔名 + var fileExt = Path.GetExtension(FileName); + //為避免使用者上傳的檔案名稱發生重複,重新給一個亂數名稱 + NewName = Path.GetRandomFileName() + fileExt; + //指定要寫入的路徑、檔名和副檔名 + FilePath = $"\\LABFile\\";//本機目錄 + + if (!System.IO.Directory.Exists(_env.WebRootPath + FilePath)) + { + System.IO.Directory.CreateDirectory(_env.WebRootPath + FilePath); + } + using (var stream = new FileStream(_env.WebRootPath + FilePath + NewName, FileMode.Create)) + { + await formFile.CopyToAsync(stream); + } + model.IMAGE_NAME = FilePath+ NewName; + } + } + else + { + ModelState.AddModelError("error", "請選擇要上傳的圖片檔案"); + } + result = await _labApi.PostLabelTemplateMaster(JsonConvert.SerializeObject(model)); @@ -316,12 +352,40 @@ namespace AMESCoreStudio.Web.Controllers //頁面提交,id=0 添加,id>0 修改 [HttpPost] - public async Task LAB002USaveAsync(LabelTemplateMaster model) + public async Task LAB002USaveAsync(LabelTemplateMaster model, IFormFile formFile) { if (ModelState.IsValid) { IResultModel result; + string FileName = string.Empty; + string NewName = string.Empty; + string FilePath = string.Empty; + if (formFile != null) + { + if (formFile.Length > 0) + { + //取得使用者上傳檔案的原始檔名 + FileName = Path.GetFileName(formFile.FileName); + //取原始檔名中的副檔名 + var fileExt = Path.GetExtension(FileName); + //為避免使用者上傳的檔案名稱發生重複,重新給一個亂數名稱 + NewName = Path.GetRandomFileName() + fileExt; + //指定要寫入的路徑、檔名和副檔名 + FilePath = $"\\LABFile\\";//本機目錄 + + if (!System.IO.Directory.Exists(_env.WebRootPath + FilePath)) + { + System.IO.Directory.CreateDirectory(_env.WebRootPath + FilePath); + } + using (var stream = new FileStream(_env.WebRootPath + FilePath + NewName, FileMode.Create)) + { + await formFile.CopyToAsync(stream); + } + model.IMAGE_NAME = FilePath + NewName; + } + } + result = await _labApi.PutLabeTemplateMaster(JsonConvert.SerializeObject(model)); diff --git a/AMESCoreStudio.Web/Views/LAB/LAB002C.cshtml b/AMESCoreStudio.Web/Views/LAB/LAB002C.cshtml index fa6f534a..6fce6de9 100644 --- a/AMESCoreStudio.Web/Views/LAB/LAB002C.cshtml +++ b/AMESCoreStudio.Web/Views/LAB/LAB002C.cshtml @@ -49,7 +49,7 @@
- +
@@ -70,14 +70,14 @@
- +
- +
@@ -128,6 +128,7 @@ CheckMatnr($("#LABEL_MATNR").val()); }); + function input(e) { if (e.keyCode == 13) { var data = $("#LABEL_MATNR").val(); @@ -152,7 +153,7 @@ success: function (result) { if (!result.data) { // alert("查無此Label料號:" + Matnr); - alert(Matnr +result.msg); + alert(Matnr +" error : "+ result.msg); } else { insertMaster2("T"); @@ -170,6 +171,37 @@ function insertMaster2(status) { + var labelFile = $("#LABEL_FILE").val(); + if (labelFile) { + var formData = new FormData($("#modelform")[0]); + + $.ajax({ + url: "/LAB/LAB002CSave", + type: "POST", + data: formData, + processData: false, // 不要处理数据 + contentType: false, // 不设置内容类型 + success: function (response) { + // 处理成功响应 + insertList(response.msg); + }, + error: function (response) { + // 处理错误 + var errorMSG = document.getElementById('errorMSG'); + errorMSG.innerHTML = response.msg; + } + }); + + } + else { + + alert("請選擇標籤檔案!"); + } + + } + + function insertMaster3(status) { + var labelFile = $("#LABEL_FILE").val(); if (labelFile) { var data = $("#LABEL_MATNR").val(); @@ -181,6 +213,7 @@ var UpdateUserID = $("#UPDATE_USERID").val(); var CreateDate = $("#CREATE_DATE").val(); var UpdateDate = $("#UPDATE_DATE").val(); + var imageFile = $("#fileInputimage").prop('files')[0]; // 获取文件对象 $.post("/LAB/LAB002CSave", { TEMPLATE_ID: templateID, @@ -192,7 +225,8 @@ CREATE_USERID: CreateUserID, UPDATE_USERID: UpdateUserID, CREATE_DATE: CreateDate, - UPDATE_DATE: UpdateDate + UPDATE_DATE: UpdateDate, + formFile: imageFile }, function (data) { if (data.success) { diff --git a/AMESCoreStudio.Web/Views/LAB/LAB002U.cshtml b/AMESCoreStudio.Web/Views/LAB/LAB002U.cshtml index 7fb0e712..f7b0c003 100644 --- a/AMESCoreStudio.Web/Views/LAB/LAB002U.cshtml +++ b/AMESCoreStudio.Web/Views/LAB/LAB002U.cshtml @@ -41,14 +41,14 @@
- +
- +
@@ -69,14 +69,14 @@
- +
- +
@@ -142,7 +142,38 @@ } }; + function insertMaster2(status) { + + var labelFile = $("#LABEL_FILE").val(); + if (labelFile) { + var formData = new FormData($("#modelform")[0]); + var templateID = $("#TEMPLATE_ID").val(); + $.ajax({ + url: "/LAB/LAB002USave", + type: "POST", + data: formData, + processData: false, // 不要处理数据 + contentType: false, // 不设置内容类型 + success: function (response) { + // 处理成功响应 + DeleteList(templateID); + }, + error: function (response) { + // 处理错误 + var errorMSG = document.getElementById('errorMSG'); + errorMSG.innerHTML = response.msg; + } + }); + + } + else { + + alert("請選擇標籤檔案!"); + } + + } + function insertMaster3(status) { var data = $("#LABEL_MATNR").val(); if (data) {