You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
227 lines
7.7 KiB
227 lines
7.7 KiB
///-----------------------------------------------------------------------
|
|
/// <copyright file="FileUploadHelper.cs" company="Origtek">
|
|
/// ArsenalDBMgr belongs to Copyright (c) Origtek. All rights reserved.
|
|
/// 程式代號: FileUploadHelper
|
|
/// 程式名稱: 檔案上傳Helper
|
|
/// 程式說明: 提供上傳功能
|
|
/// 起始作者: Hercules
|
|
/// 起始日期: 2017/05/15
|
|
/// 最新修改人: Hercules
|
|
/// 最新修日期: 2017/05/18 17:45:54
|
|
/// </copyright>
|
|
///-----------------------------------------------------------------------
|
|
#region 程式異動記錄
|
|
/// xx.YYYY/MM/DD VER AUTHOR COMMENTS(說明修改的內容)
|
|
/// 01.2017/05/15 1.000 Hercules 檔案上傳 獨立成Helper(RV.4293)
|
|
/// 02.2017/05/18 1.001 Hercules 補程式Header註解(RV.4375)
|
|
#endregion
|
|
|
|
using CounsellorBL.BLStructure;
|
|
using MonumentDefine;
|
|
using Newtonsoft.Json.Linq;
|
|
using OT.COM.ArsenalDB;
|
|
using OT.COM.SignalerMessage;
|
|
using SoldierData.EnterprizeV4;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
|
|
namespace CounsellorBL.Helper
|
|
{
|
|
/// <summary>
|
|
/// 類別名稱: FileUploadHelper
|
|
/// 類別說明 檔案上傳Helper
|
|
/// 起始作者: Hercules
|
|
/// 起始日期: 2017/05/15
|
|
/// 最新修改人: Hercules
|
|
/// 最新修改日: 2017/05/15
|
|
/// </summary>
|
|
public partial class FileUploadHelper : DBService
|
|
{
|
|
|
|
/// <summary>
|
|
/// 檔案上傳Helper必要資訊資料結構實體
|
|
/// </summary>
|
|
private readonly FileUploadInfo _fuiInst = null;
|
|
|
|
public override string MainTable => typeof(tb_sys_uploadlog).Name;
|
|
|
|
/// <summary>
|
|
/// 函式名稱:FileUploadHelper
|
|
/// 函式說明:建構式
|
|
/// 起始作者:
|
|
/// 起始日期:
|
|
/// 最新修改人:
|
|
/// 最新修改日:
|
|
/// </summary>
|
|
/// <param name="i_fuiInst">檔案上傳Helper必要資訊資料結構,可透過 DBService.GetFileUploadInfo快速取得</param>
|
|
/// 參數說明
|
|
/// </param>
|
|
/// <returns>
|
|
/// 回傳
|
|
/// </returns>
|
|
public FileUploadHelper(FileUploadInfo i_fuiInst, CRequestMessage i_crm)
|
|
{
|
|
OriRequest = i_crm;
|
|
_fuiInst = i_fuiInst;
|
|
}
|
|
/// <summary>
|
|
/// 函式名稱:UploadFile
|
|
/// 函式說明:上傳檔案
|
|
/// 起始作者:
|
|
/// 起始日期:
|
|
/// 最新修改人:
|
|
/// 最新修改日:
|
|
/// </summary>
|
|
/// <param name="i_crm">參數說明i_crmInput(Object):前端上傳所需資訊</param>
|
|
/// <param name="o_lRes">上傳後所有檔案路徑</param>
|
|
/// <param name="i_sfolderpath">指定上傳路徑</param>
|
|
/// <returns>錯誤訊息,若無則回傳Null</returns>
|
|
/// <summary>
|
|
public string UploadFile(CRequestMessage i_crm, out List<tb_sys_uploadlog> o_lRes, string i_sfolderpath)
|
|
{
|
|
o_lRes = null;
|
|
return i_crm != null ? getUploadFiles(i_crm, out o_lRes, i_sfolderpath) : MessageWording.PARAM_NOT_EXPECTED;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 函式名稱:getUploadFiles
|
|
/// 函式說明:實際檔案上傳程式
|
|
/// 起始作者:
|
|
/// 起始日期:
|
|
/// 最新修改人:
|
|
/// 最新修改日:
|
|
/// </summary>
|
|
/// <param name="i_crm">參數說明i_crmInput(Object):前端上傳所需資訊</param>
|
|
/// <param name="o_lRes">上傳後所有檔案路徑</param>
|
|
/// <param name="i_sfolderpath">指定上傳路徑</param>
|
|
/// <returns>錯誤訊息,若無則回傳Null</returns>
|
|
/// <summary>
|
|
private string getUploadFiles(CRequestMessage i_crmInput, out List<tb_sys_uploadlog> o_lRes, string i_sfolderpath = null)
|
|
{
|
|
List<tb_sys_uploadlog> lRes = null;
|
|
string sMsg = null;
|
|
|
|
do
|
|
{
|
|
List<UploadFiles> lResTemp = new List<UploadFiles>();
|
|
|
|
if (!(i_crmInput.param[BLWording.FILES] is JArray jaObjects) || jaObjects.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (_fuiInst.Logger == null || _fuiInst.RootPath == null)
|
|
{
|
|
sMsg = MessageWording.PARAM_NOT_EXPECTED;
|
|
break;
|
|
}
|
|
|
|
string sFolderPath = generateUploadFolderPath(i_sfolderpath);
|
|
|
|
List<tb_sys_uploadlog> lUploadLogs = new List<tb_sys_uploadlog>();
|
|
|
|
foreach (JToken jo in jaObjects)
|
|
{
|
|
UploadFiles uf = new UploadFiles()
|
|
{
|
|
UploadName = jo[BLWording.NAME].ToString(),
|
|
};
|
|
|
|
string sBase64 = (string)jo[BLWording.BSTRING];
|
|
int nIdx = sBase64.IndexOf(',', StringComparison.OrdinalIgnoreCase);
|
|
uf.FileMeta = sBase64.Substring(0, nIdx);
|
|
var fileName = generateUploadFolderName(i_crmInput);
|
|
|
|
byte[] bRawData = Convert.FromBase64String(sBase64.Substring(nIdx + 1));
|
|
|
|
string sFilePath = Path.Combine(sFolderPath, string.Format(CultureInfo.CurrentCulture, "{0}{1}", fileName, Path.GetExtension(jo[BLWording.NAME].ToString())));
|
|
|
|
System.IO.File.WriteAllBytes(sFilePath, bRawData);
|
|
|
|
uf.FilePath = sFilePath;
|
|
|
|
|
|
lResTemp.Add(uf);
|
|
|
|
lUploadLogs.Add(new tb_sys_uploadlog()
|
|
{
|
|
uid = Guid.NewGuid().ToString(),
|
|
module = i_crmInput.module,
|
|
name = jo[BLWording.NAME].ToString(),
|
|
path = sFilePath.Substring(_fuiInst.RootPath.Length)
|
|
});
|
|
}
|
|
|
|
lRes = lUploadLogs;
|
|
|
|
if (lRes.Count > 0)
|
|
{
|
|
List<Command> lcInsert = new List<Command>();
|
|
|
|
foreach (tb_sys_uploadlog ul in lUploadLogs)
|
|
{
|
|
lcInsert.Add(Command.SetupInsertCmd(ul));
|
|
}
|
|
|
|
this.OriRequest = i_crmInput;
|
|
|
|
ArsenalInterface ai = ArsenalDBMgr.GetInst(lcInsert[0], GetDefaultSystemColumnInfo());
|
|
|
|
ai.RunEditCmds(lcInsert);
|
|
}
|
|
}
|
|
while (false);
|
|
|
|
|
|
o_lRes = lRes;
|
|
|
|
|
|
return sMsg;
|
|
}
|
|
|
|
public string UploadLocalFile(string i_sLocalFile, out tb_sys_uploadlog o_ulData)
|
|
{
|
|
string sMsg = null;
|
|
tb_sys_uploadlog ulRes = null;
|
|
|
|
|
|
do
|
|
{
|
|
if (i_sLocalFile == null)
|
|
{
|
|
sMsg = MessageWording.PARAM_NOT_EXPECTED;
|
|
break;
|
|
}
|
|
string[] saPath = i_sLocalFile.Split("\\/".ToCharArray());
|
|
tb_sys_uploadlog ulInsert = new tb_sys_uploadlog()
|
|
{
|
|
uid = Guid.NewGuid().ToString(),
|
|
module = OriRequest?.module,
|
|
path = i_sLocalFile,
|
|
name = saPath[^1]
|
|
};
|
|
|
|
Command cInsert = Command.SetupInsertCmd(ulInsert);
|
|
|
|
ArsenalInterface ai = ArsenalDBMgr.GetInst(cInsert, GetDefaultSystemColumnInfo());
|
|
|
|
ai.RunEditSingleCmd(cInsert);
|
|
|
|
if (!cInsert.IsSuccess)
|
|
{
|
|
sMsg = GetLastErrorCode(cInsert);
|
|
break;
|
|
}
|
|
|
|
ulRes = ulInsert;
|
|
}
|
|
while (false);
|
|
|
|
|
|
o_ulData = ulRes;
|
|
return sMsg;
|
|
}
|
|
}
|
|
}
|