using DAL; using DBUtility; using Model; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using SQLHelper; using System; using System.Text; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Http; using WebAPI; using WebAPI.Controllers.SCGL.日计划管理; using WebAPI.Models; using WebAPI.Service; using SyntacticSugar.constant; using System.Text.RegularExpressions; namespace WebAPI.Controllers { public class WebFileController : ApiController { //通用文件上传控制器 public string sWhere = ""; public WebServer webserver = new WebServer(); public DataSet ds = new DataSet(); private json objjson = new json(); private json objJsonResult = new json(); SQLHelper.ClsCN oCN = new SQLHelper.ClsCN(); Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter(); string fileip = System.Configuration.ConfigurationManager.AppSettings["FileIP"]; #region 文件上传 [Route("WebFile/UploadFile_WebFile")] [HttpPost] public object UploadFile_WebFile() { string path = ""; string HBillType = HttpContext.Current.Request.Params["HBillType"]; //单据类型 string HBillNo = HttpContext.Current.Request.Params["HBillNo"]; //单据号 string HRemark = HttpContext.Current.Request.Params["HRemark"]; //备注 string HUserName = HttpContext.Current.Request.Params["HUserName"]; //创建人 HttpPostedFile files = HttpContext.Current.Request.Files["file"]; // === 动态确定基础文件夹(根据单据类型) === string baseFolder = "Files/WebFile/"; // 默认路径 if (HBillType == "3907") baseFolder = "Files/Sb_EquipConkBookBill/"; // 设备故障登记单 else if (HBillType == "3920") baseFolder = "Files/Sb_EquipRepairSignBill/"; // 设备维修签到单 else if (HBillType == "3909") baseFolder = "Files/Sb_EquipRepairSendWorkBill/"; // 设备维修派工单 else if (HBillType == "3910") baseFolder = "Files/Sb_EquipRepairWorkBill/"; // 设备维修记录单 else if (HBillType == "3911") baseFolder = "Files/Sb_EquipRepairCheckBill/"; // 设备维修验收单 else if (HBillType == "3815") baseFolder = "Files/Sc_MouldConkBookBill/"; // 模具故障登记单 else if (HBillType == "3843") baseFolder = "Files/Sc_MouldRepairSendWorkBill/"; // 模具维修派工单 else if (HBillType == "3807") baseFolder = "Files/Sc_MouldRepairWorkBill/"; // 模具维修记录单 else if (HBillType == "3817") baseFolder = "Files/Sc_MouldRepairCheckBill/"; // 模具维修验收 path = HttpContext.Current.Server.MapPath("~/../" + baseFolder + HBillNo); dynamic dyResult = UploadFile_WebFile(files, path, HBillNo, HRemark, HUserName, baseFolder); if (dyResult != null && dyResult.result == 1) { objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "上传成功!"; objJsonResult.data = null; return objJsonResult; } else { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = dyResult.returnval; objJsonResult.data = null; return objJsonResult; } } public dynamic UploadFile_WebFile(HttpPostedFile files, string path, string HBillNo, string HRemark, string HUserName, string baseFolder) { dynamic Result_Ob = new { result = 1, returnval = "上传成功!" }; string filePath = Path.GetFullPath(files.FileName);//文件上传路径 string fileExtension = Path.GetExtension(files.FileName);// 文件扩展名 string filename = files.FileName;//文件名 string fileSavePath = path;// 上传保存路径 int filesize = files.ContentLength;//获取上传文件的大小单位为字节byte int Maxsize = 40000 * 1024;//定义上传文件的最大空间大小为40M // 构建数据库存储的相对路径(/ 开头,如 /Files/WebFile/BillNo/) string relativeUrlPath = "/" + baseFolder.TrimStart('~').TrimStart('/') + HBillNo + "/"; try { if (files == null || files.ContentLength <= 0) { Result_Ob = new { result = 0, returnval = "文件不能为空!" }; return Result_Ob; } if (filesize >= Maxsize) { Result_Ob = new { result = 0, returnval = "上传文件超过40M,不能上传!" }; return Result_Ob; } string fileurl = Path.Combine(fileSavePath, filename); if (Directory.Exists(fileurl) == true) //如果存在重名文件就提示 { Result_Ob = new { result = 0, returnval = "存在同名文件!" }; return Result_Ob; } //删除数据表数据 ds = oCN.RunProcReturn("delete from MES_AccessoriesList where HSourceBillNo ='" + HBillNo + "' and HFileName='" + filename + "'", "MES_AccessoriesList"); if (Directory.Exists(path)) { File.Delete(fileurl); //删除指定文件 files.SaveAs(fileurl); string StrPath = relativeUrlPath + filename; // 数据库存储的相对路径+文件名 if (File.Exists(fileurl)) { //写入数据表(HFilePath 使用相对路径) oCN.RunProc("Insert into MES_AccessoriesList (HFileName,HFilePath,HFilePath_Cus,HFileType" + ",HLoadMan,HLoadDate,HRemark,HVerNum,HFileSize" + ",HFileClsID,HSourceBillNo" + ") values('" + filename.ToString() + "','" + StrPath.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString() + "'" + ",'" + HUserName + "',getdate(),'" + HRemark + "','V1','" + filesize + "','" + 0 + "','" + HBillNo + "') "); } else { Result_Ob = new { result = 0, returnval = "上传失败!此文件为恶意文件" }; } } else { Directory.CreateDirectory(fileSavePath); //添加文件夹 files.SaveAs(fileurl); string StrPath = relativeUrlPath + filename; if (File.Exists(fileurl)) { oCN.RunProc("Insert into MES_AccessoriesList (HFileName,HFilePath,HFilePath_Cus,HFileType" + ",HLoadMan,HLoadDate,HRemark,HVerNum,HFileSize" + ",HFileClsID,HSourceBillNo" + ") values('" + filename.ToString() + "','" + StrPath.ToString() + "','" + filePath.ToString() + "','" + fileExtension.ToString() + "'" + ",'" + HUserName + "',getdate(),'" + HRemark + "','V1','" + filesize + "','" + 0 + "','" + HBillNo + "') "); } else { Result_Ob = new { result = 0, returnval = "上传失败!此文件为恶意文件" }; } } } catch (Exception e) { Result_Ob = new { result = 0, returnval = e.Message }; } return Result_Ob; } /// /// 根据单据号查找上传文件列表 /// [Route("WebFile/WebFileList")] [HttpGet] public object WebFileList(string HBillNo) { try { var request = HttpContext.Current.Request; string baseUrl = $"{request.Url.Scheme}://{request.Url.Authority}/"; // 2. 直接用数据库已有的 HFilePath + HFileName 拼接完整 URL // HFilePath 示例:/Files/WebFile/BillNo-001/ string sql = $@"SELECT *, '{baseUrl}' + HFilePath AS url FROM MES_AccessoriesList WHERE HSourceBillNo = '{HBillNo.Replace("'", "''")}'"; ds = oCN.RunProcReturn(sql, "MES_AccessoriesList"); // 3. 正常返回数据 objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "Sucess!"; objJsonResult.data = ds.Tables[0]; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.Message; objJsonResult.data = null; return objJsonResult; } } /// /// 根据ID,单据号,文件名 删除文件(同时删除数据库记录和服务器物理文件) /// [Route("WebFile/WebFile_DeleteFilelist")] [HttpGet] public object WebFile_DeleteFilelist(string HItemID) { try { // 1. 先从数据库获取该文件的存储路径(HFilePath 字段在上传时已存储了完整的相对路径+文件名) DataSet dsFile = oCN.RunProcReturn( "select HFilePath from MES_AccessoriesList where HItemID =" + HItemID, "MES_AccessoriesList"); // 2. 构建物理路径并删除服务器上的文件 if (dsFile != null && dsFile.Tables[0].Rows.Count > 0) { string relativePath = dsFile.Tables[0].Rows[0]["HFilePath"].ToString(); // 如 /Files/WebFile/BillNo/xxx.pdf // 去掉开头的 /,然后拼到网站根目录上一级 string physicalPath = HttpContext.Current.Server.MapPath("~/../" + relativePath.TrimStart('/')); if (File.Exists(physicalPath)) { File.Delete(physicalPath); } } // 3. 删除数据库记录 oCN.RunProc("delete from MES_AccessoriesList where HItemID =" + HItemID); objJsonResult.code = "1"; objJsonResult.count = 1; objJsonResult.Message = "删除成功!"; objJsonResult.data = null; return objJsonResult; } catch (Exception e) { objJsonResult.code = "0"; objJsonResult.count = 0; objJsonResult.Message = "Exception!" + e.ToString(); objJsonResult.data = null; return objJsonResult; } } #endregion } }