From 1d3b601cc12aa27915f21a11110a6d84df430a7b Mon Sep 17 00:00:00 2001 From: dev01 Date: Thu, 10 Nov 2022 14:34:53 +0800 Subject: [PATCH] =?UTF-8?q?[FrontedWebApi][File]=20=E4=B8=8B=E8=BC=89?= =?UTF-8?q?=E6=AA=94=E6=A1=88=E7=A8=8B=E5=BA=8F=E5=BB=BA=E7=BD=AE=20|=20[?= =?UTF-8?q?=E5=9C=96=E8=B3=87=E7=AE=A1=E7=90=86]=20=E7=B7=A8=E8=BC=AF?= =?UTF-8?q?=E6=99=82=E5=88=AA=E9=99=A4=E6=AA=94=E6=A1=88=E5=88=A4=E6=96=B7?= =?UTF-8?q?=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/FileController.cs | 64 +++++++++++++++++++ .../ApiControllers/GraphManageController.cs | 20 +++--- FrontendWebApi/appsettings.json | 4 +- 3 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 FrontendWebApi/ApiControllers/FileController.cs diff --git a/FrontendWebApi/ApiControllers/FileController.cs b/FrontendWebApi/ApiControllers/FileController.cs new file mode 100644 index 0000000..849fe7c --- /dev/null +++ b/FrontendWebApi/ApiControllers/FileController.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Repository.BackendRepository.Interface; +using Repository.FrontendRepository.Interface; +using System; +using System.IO; + +namespace FrontendWebApi.ApiControllers +{ + public class FileController : MyBaseApiController + { + private readonly IBackendRepository backendRepository; + private readonly IFrontendRepository frontendRepository; + + private string graphManageFileSaveAsPath = ""; + private string operationFileSaveAsPath = ""; + + + public FileController(IBackendRepository backendRepository, IFrontendRepository frontendRepository) + { + this.backendRepository = backendRepository; + this.frontendRepository = frontendRepository; + graphManageFileSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "graph_manage"); + operationFileSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "operation"); + } + public bool Download([FromQuery]string type , string savename, string oriname) + { + var path = ""; + if (type == "graph") + { + path = Path.Combine(graphManageFileSaveAsPath, savename); + } + else if (type == "operation") + { + path = Path.Combine(operationFileSaveAsPath, savename); + } + + if (System.IO.File.Exists(path) && path != "") + { + try + { + FileInfo xpath_file = new FileInfo(path); //要 using System.IO; + + // 將傳入的檔名以 FileInfo 來進行解析(只以字串無法做) + HttpContext.Response.Clear(); //清除buffer + HttpContext.Response.ContentType = "application/octet-stream"; + // 檔案類型還有下列幾種"application/pdf"、"application/vnd.ms-excel"、"text/xml"、"text/HTML"、"image/JPEG"、"image/GIF" + HttpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(oriname, System.Text.Encoding.UTF8)); + // 考慮 utf-8 檔名問題,以 out_file 設定另存的檔名 + HttpContext.Response.Headers.Add("Content-Length", xpath_file.Length.ToString()); //表頭加入檔案大小 + HttpContext.Response.SendFileAsync(xpath_file.FullName); + + return true; + + } + catch (Exception) + { return false; } + + } + else + return false; + } + } +} diff --git a/FrontendWebApi/ApiControllers/GraphManageController.cs b/FrontendWebApi/ApiControllers/GraphManageController.cs index 7d93b4b..8d701f8 100644 --- a/FrontendWebApi/ApiControllers/GraphManageController.cs +++ b/FrontendWebApi/ApiControllers/GraphManageController.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using System; using System.Linq; +using System.Web; using Repository.BackendRepository.Interface; using Repository.FrontendRepository.Interface; using System.IO; @@ -109,7 +110,7 @@ namespace FrontendWebApi.ApiControllers var param = new { @graph_manage_layer1 = graph_manage_layer1, graph_manage_layer2 = graph_manage_layer2, @sub_system_tag = gi.sub_system_tag, @main_system_tag = gi.main_system_tag }; graManList = await backendRepository.GetAllAsync(sqlString, param); - + if (gi.keyWord != null) { var wParam = new { @graph_manage_layer1 = graph_manage_layer1, graph_manage_layer2 = graph_manage_layer2, @sub_system_tag = gi.sub_system_tag, @main_system_tag = gi.main_system_tag, @keyWord = gi.keyWord }; @@ -149,7 +150,7 @@ namespace FrontendWebApi.ApiControllers WHERE gm.id = @id ORDER BY gm.priority, gm.created_at desc"; - var param = new { @id = gi.id , @graph_manage_layer1 = graph_manage_layer1, @graph_manage_layer2 = graph_manage_layer2 }; + var param = new { @id = gi.id, @graph_manage_layer1 = graph_manage_layer1, @graph_manage_layer2 = graph_manage_layer2 }; graManList = await backendRepository.GetOneAsync(sqlString, param); @@ -218,7 +219,7 @@ namespace FrontendWebApi.ApiControllers sWhere = @$"deleted = 0 AND id = @id"; gm = await backendRepository.GetOneAsync("graph_manage", sWhere, new { @id = gii.id }); - + if (gm == null) { apiResult.Code = "0001"; @@ -232,14 +233,14 @@ namespace FrontendWebApi.ApiControllers //原設計圖修改 var new_guid = Guid.NewGuid(); //刪除原本檔案 - if (gm.oriSavName != null) + if (gm.oriSavName != null && gii.oriOrgName == null) { FolderFunction folderFunction = new FolderFunction(); folderFunction.DeleteFile(Path.Combine(graphManageFileSaveAsPath, gm.oriSavName)); } string fileName = null; - if (gii.oriOrgName != null) + if (gii.oriOrgName != null && gii.oriFile != null) { fileName = new_guid + "." + gii.oriOrgName.Split('.')[1]; @@ -257,13 +258,13 @@ namespace FrontendWebApi.ApiControllers //竣工圖修改 new_guid = Guid.NewGuid(); //刪除原本檔案 - if (gm.donSavName != null) + if (gm.donSavName != null && gii.donOrgName == null) { var folderFunction = new FolderFunction(); folderFunction.DeleteFile(Path.Combine(graphManageFileSaveAsPath, gm.donSavName)); } - if (gii.donOrgName != null) + if (gii.donOrgName != null && gii.donFile != null) { fileName = new_guid + "." + gii.donOrgName.Split('.')[1]; @@ -347,7 +348,7 @@ namespace FrontendWebApi.ApiControllers if (gii.donOrgName != null) { var new_guid = Guid.NewGuid(); - + var fileName = new_guid + "." + gii.donOrgName.Split('.')[1]; var fullPath = Path.Combine(graphManageFileSaveAsPath, fileName); @@ -369,7 +370,7 @@ namespace FrontendWebApi.ApiControllers graph_manage.Add("@name", gii.name); graph_manage.Add("@deleted", 0); graph_manage.Add("@priority", newPriority + 1); - graph_manage.Add("@created_by", myUser.userinfo_guid ); + graph_manage.Add("@created_by", myUser.userinfo_guid); graph_manage.Add("@created_at", DateTime.Now); await backendRepository.AddOneByCustomTable(graph_manage, "graph_manage"); @@ -387,5 +388,6 @@ namespace FrontendWebApi.ApiControllers return apiResult; } + } } \ No newline at end of file diff --git a/FrontendWebApi/appsettings.json b/FrontendWebApi/appsettings.json index 5ce7c08..af29da8 100644 --- a/FrontendWebApi/appsettings.json +++ b/FrontendWebApi/appsettings.json @@ -7,8 +7,8 @@ } }, "JwtSettings": { - "Issuer": "Admin", //oe - "SignKey": "TaipeiDome123456", //ñ//̤16r + "Issuer": "Admin", //�o�e�� + "SignKey": "TaipeiDome123456", //ñ��//�̤�16�r�� "JwtLifeSeconds": 1800 }, "DBConfig": {