SVN r1158
SVN-Revision: r1158
This commit is contained in:
@@ -3896,138 +3896,7 @@ namespace Lskj.PubBomImport
|
||||
}
|
||||
return null; // 未找到合并区域
|
||||
}
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <returns></returns>
|
||||
private bool UploadFile(string filePath, string idValue, out string msg, out string outFileId)
|
||||
{
|
||||
bool isUpload = false;
|
||||
string File = filePath;
|
||||
string returnMsg = "";
|
||||
string fileId = "";
|
||||
string postUrl = "";
|
||||
try
|
||||
{
|
||||
//获取文件信息
|
||||
FileInfo fileInfo = new FileInfo(File);
|
||||
string suffix = Path.GetExtension(File);
|
||||
string name = fileInfo.Name;
|
||||
DateTime creationTime = fileInfo.CreationTime;
|
||||
byte[] fileBytes = ConvertFileToBytes(File);
|
||||
string fileStream = Convert.ToBase64String(fileBytes);
|
||||
string userId = ERPInfo.Instance.UserId;
|
||||
DateTime uploadTime = DateTime.Now;
|
||||
string speciesno = "010102";
|
||||
// 检查扩展名是否为 .xlsx 或 .xls
|
||||
if (suffix.Equals(".xlsx", StringComparison.OrdinalIgnoreCase) ||
|
||||
suffix.Equals(".xls", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
speciesno = "010101";
|
||||
}
|
||||
//上传文件到服务器目录
|
||||
postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&idValue={2}&speciesno={8}&folder={3}&totsize={4}&position={5}&filename={6}&method={7}&confirm=1";
|
||||
if (!string.IsNullOrEmpty(idValue))
|
||||
{
|
||||
string token = "";
|
||||
string loginUrl = $"{SystemInfo.Instance.OAUrl}/Api/SysUserAjaxApi.ashx";
|
||||
HttpTools.setting("application/x-www-form-urlencoded", null, null, HttpTools.Encode.UTF8);
|
||||
Dictionary<string, string> loginPmsDic = new Dictionary<string, string>();
|
||||
loginPmsDic.Add("method", "Login");
|
||||
loginPmsDic.Add("username", System.Web.HttpUtility.UrlEncode(ERPInfo.Instance.UserName));
|
||||
loginPmsDic.Add("password", System.Web.HttpUtility.UrlEncode(ERPInfo.Instance.InPassWord));
|
||||
HttpWebResponse loginResponse = HttpTools.Post(loginUrl, "", loginPmsDic, HttpTools.Method.POST, out CookieCollection loginCookie, out string loginResult);
|
||||
if (loginResponse != null && !string.IsNullOrEmpty(loginResult))
|
||||
{
|
||||
JObject jObject = JsonConvert.DeserializeObject<JObject>(loginResult);
|
||||
if (jObject.ContainsKey("success"))
|
||||
{
|
||||
if ((jObject["success"] + "").Equals("True"))
|
||||
{
|
||||
if (jObject.ContainsKey("token"))
|
||||
{
|
||||
token = jObject["token"] + "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
//string selectSql = $"select * from P_fm_FileTab where parentid in (select dirid from P_fm_DirectoryTab where pid = '{idValue}' and dllcoid = '1000202') and speciesno = '{speciesno}' and sname = '{name.Replace(" ", "")}'";
|
||||
//DataTable deleteTable = BaseImpl.GetDataTableResult(selectSql);
|
||||
//if (deleteTable != null && deleteTable.Rows.Count > 0)
|
||||
//{
|
||||
// foreach (DataRow deleteRow in deleteTable.Rows)
|
||||
// {
|
||||
// string field = HttpUtility.UrlEncode(deleteRow["fileId"] + "");
|
||||
// string webpath = HttpUtility.UrlEncode(deleteRow["webpath"] + "");
|
||||
// //删除文件
|
||||
// string deleteUrl = "{0}Api/FileUploadApi.ashx?fileId={1}&filename={2}&method={3}";
|
||||
// deleteUrl = string.Format(deleteUrl, SystemInfo.Instance.OAUrl, field, webpath, "DoDelete");
|
||||
// HttpHelper.Setting("application/x-www-form-urlencoded", null, null);
|
||||
// Dictionary<string, string> deleteHeaderDic = new Dictionary<string, string>();
|
||||
// deleteHeaderDic.Add("Authorization", $"Bearer {token}");
|
||||
// HttpWebResponse deleteWebResponse = HttpHelper.Post(deleteUrl, fileStream, null, deleteHeaderDic, out string deleteResult);
|
||||
// }
|
||||
//}
|
||||
postUrl = string.Format(postUrl, SystemInfo.Instance.OAUrl, "1000202", System.Web.HttpUtility.UrlEncode(idValue), "file", fileBytes.Length, 0, System.Web.HttpUtility.UrlEncode(name), "DoWebUpload", speciesno);
|
||||
HttpTools.setting("application/x-www-form-urlencoded", null, null);
|
||||
string result = "";
|
||||
CookieCollection cookieCollection = null;
|
||||
Dictionary<string, string> headerDic = new Dictionary<string, string>();
|
||||
headerDic.Add("Authorization", $"Bearer {token}");
|
||||
HttpWebResponse webResponse = HttpTools.Post(postUrl, fileStream, null, headerDic, HttpTools.Method.POST, out cookieCollection, out result);
|
||||
if (webResponse != null && !string.IsNullOrEmpty(result))
|
||||
{
|
||||
JObject jsonObject = (JObject)JsonConvert.DeserializeObject(result);
|
||||
string isSuccess = jsonObject["success"] + "";
|
||||
if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
if (jsonObject.ContainsKey("other"))
|
||||
{
|
||||
fileId = jsonObject["other"] + "";
|
||||
}
|
||||
isUpload = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (jsonObject.ContainsKey("msg"))
|
||||
{
|
||||
returnMsg = jsonObject["msg"] + "";
|
||||
}
|
||||
isUpload = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
returnMsg = "上传请求失败";
|
||||
isUpload = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
returnMsg = loginResult;
|
||||
isUpload = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
returnMsg = "上传节点值不能为空";
|
||||
isUpload = false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
returnMsg = ex.Message;
|
||||
throw new Exception($"文件{Path.GetFileName(filePath)}上传失败\r\n{ex.Message}");
|
||||
isUpload = false;
|
||||
}
|
||||
msg = returnMsg;
|
||||
outFileId = fileId;
|
||||
return isUpload;
|
||||
}
|
||||
|
||||
private bool UploadFileToAudit(string filePath, string idValue, string speciesno, out string msg, out string outFileId)
|
||||
{
|
||||
bool isUpload = false;
|
||||
@@ -4054,17 +3923,20 @@ namespace Lskj.PubBomImport
|
||||
code = namematch.Groups[1].Value;
|
||||
}
|
||||
//上传文件到服务器目录
|
||||
postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&idValue={2}&speciesno={8}&folder={3}&totsize={4}&position={5}&filename={6}&method={7}&confirm=1";
|
||||
//postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&idValue={2}&speciesno={8}&folder={3}&totsize={4}&position={5}&filename={6}&method={7}&confirm=1";
|
||||
|
||||
postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&idValue={2}&speciesno={8}&folder={3}&totsize={4}&position={5}&filename={6}&method={7}&comfirm={9}&encode={10}&gzip=true&uploadToken={11}&__tsp={12}";
|
||||
|
||||
if (!string.IsNullOrEmpty(idValue))
|
||||
{
|
||||
string token = "";
|
||||
string loginUrl = $"{SystemInfo.Instance.OAUrl}/Api/SysUserAjaxApi.ashx";
|
||||
HttpTools.setting("application/x-www-form-urlencoded", null, null, HttpTools.Encode.UTF8);
|
||||
HttpHelper.Setting("application/x-www-form-urlencoded", null, null, HttpHelper.Encode.UTF8);
|
||||
Dictionary<string, string> loginPmsDic = new Dictionary<string, string>();
|
||||
loginPmsDic.Add("method", "Login");
|
||||
loginPmsDic.Add("username", System.Web.HttpUtility.UrlEncode(ERPInfo.Instance.UserName));
|
||||
loginPmsDic.Add("password", System.Web.HttpUtility.UrlEncode(ERPInfo.Instance.InPassWord));
|
||||
HttpWebResponse loginResponse = HttpTools.Post(loginUrl, "", loginPmsDic, HttpTools.Method.POST, out CookieCollection loginCookie, out string loginResult);
|
||||
HttpWebResponse loginResponse = HttpHelper.Post(loginUrl, "", loginPmsDic, null, out string loginResult);
|
||||
if (loginResponse != null && !string.IsNullOrEmpty(loginResult))
|
||||
{
|
||||
JObject jObject = JsonConvert.DeserializeObject<JObject>(loginResult);
|
||||
@@ -4081,56 +3953,148 @@ namespace Lskj.PubBomImport
|
||||
}
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
//string selectSql = $"select * from P_fm_FileTab where parentid in (select dirid from P_fm_DirectoryTab where pid = '{idValue}' and dllcoid = '1000202') and speciesno = '{speciesno}' and sname = '{name.Replace(" ", "")}'";
|
||||
//DataTable deleteTable = BaseImpl.GetDataTableResult(selectSql);
|
||||
//if (deleteTable != null && deleteTable.Rows.Count > 0)
|
||||
//{
|
||||
// foreach (DataRow deleteRow in deleteTable.Rows)
|
||||
// {
|
||||
// string field = HttpUtility.UrlEncode(deleteRow["fileId"] + "");
|
||||
// string webpath = HttpUtility.UrlEncode(deleteRow["webpath"] + "");
|
||||
// //删除文件
|
||||
// string deleteUrl = "{0}Api/FileUploadApi.ashx?fileId={1}&filename={2}&method={3}";
|
||||
// deleteUrl = string.Format(deleteUrl, SystemInfo.Instance.OAUrl, field, webpath, "DoDelete");
|
||||
// HttpHelper.Setting("application/x-www-form-urlencoded", null, null);
|
||||
// Dictionary<string, string> deleteHeaderDic = new Dictionary<string, string>();
|
||||
// deleteHeaderDic.Add("Authorization", $"Bearer {token}");
|
||||
// HttpWebResponse deleteWebResponse = HttpHelper.Post(deleteUrl, fileStream, null, deleteHeaderDic, out string deleteResult);
|
||||
// }
|
||||
//}
|
||||
postUrl = string.Format(postUrl, SystemInfo.Instance.OAUrl, "1000202", System.Web.HttpUtility.UrlEncode(idValue), "file", fileBytes.Length, 0, System.Web.HttpUtility.UrlEncode(name), "DoWebUpload", speciesno);
|
||||
HttpTools.setting("application/x-www-form-urlencoded", null, null);
|
||||
string result = "";
|
||||
CookieCollection cookieCollection = null;
|
||||
int pageSize = 1 * 1024 * 1024;
|
||||
long totalSize = fileInfo.Length;
|
||||
long position = 0;
|
||||
int confirm = 1;
|
||||
int encode = 4;
|
||||
int failNum = 0;
|
||||
bool hasFail = false;
|
||||
|
||||
string uploadToken = Guid.NewGuid().ToString("N").Substring(0, 24);
|
||||
string uploadFileName = name;
|
||||
string uploadMethod = "DoWebUpload";
|
||||
|
||||
Dictionary<string, string> headerDic = new Dictionary<string, string>();
|
||||
headerDic.Add("Authorization", $"Bearer {token}");
|
||||
HttpWebResponse webResponse = HttpTools.Post(postUrl, fileStream, null, headerDic, HttpTools.Method.POST, out cookieCollection, out result);
|
||||
|
||||
using (FileStream fs = new FileStream(File, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
while (position < totalSize)
|
||||
{
|
||||
int readSize = (int)Math.Min(pageSize, totalSize - position);
|
||||
byte[] buffer = new byte[readSize];
|
||||
|
||||
int realRead = fs.Read(buffer, 0, readSize);
|
||||
if (realRead <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (realRead != readSize)
|
||||
{
|
||||
byte[] tempBytes = new byte[realRead];
|
||||
Array.Copy(buffer, tempBytes, realRead);
|
||||
buffer = tempBytes;
|
||||
readSize = realRead;
|
||||
}
|
||||
|
||||
long tsp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
|
||||
string uploadUrl = string.Format(
|
||||
postUrl,
|
||||
SystemInfo.Instance.OAUrl,
|
||||
"1000202",
|
||||
System.Web.HttpUtility.UrlEncode(idValue),
|
||||
"file",
|
||||
totalSize,
|
||||
position,
|
||||
System.Web.HttpUtility.UrlEncode(uploadFileName),
|
||||
uploadMethod,
|
||||
System.Web.HttpUtility.UrlEncode(speciesno),
|
||||
confirm,
|
||||
encode,
|
||||
uploadToken,
|
||||
tsp
|
||||
);
|
||||
|
||||
string result = "";
|
||||
HttpWebResponse webResponse = HttpHelper.PostFileChunk(uploadUrl, buffer, uploadFileName, headerDic, out result);
|
||||
|
||||
if (webResponse != null && !string.IsNullOrEmpty(result))
|
||||
{
|
||||
JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result);
|
||||
string isSuccess = jsonObject["success"] + "";
|
||||
|
||||
if (isSuccess.Equals("True", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
failNum = 0;
|
||||
|
||||
if (jsonObject.ContainsKey("data") && jsonObject["data"] != null && jsonObject["data"].Type == JTokenType.Object)
|
||||
{
|
||||
JObject dataObject = (JObject)jsonObject["data"];
|
||||
|
||||
if (dataObject.ContainsKey("FileName"))
|
||||
{
|
||||
uploadFileName = dataObject["FileName"] + "";
|
||||
}
|
||||
}
|
||||
|
||||
if (jsonObject.ContainsKey("other"))
|
||||
{
|
||||
fileId = jsonObject["other"] + "";
|
||||
BaseImpl.ExecSqlValue($"update p_fm_filetab set isdisabled = 2 where fileid = '{fileId}'");
|
||||
}
|
||||
isUpload = true;
|
||||
|
||||
position += readSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
string dataCode = jsonObject.ContainsKey("data") ? jsonObject["data"] + "" : "";
|
||||
|
||||
if (dataCode.Equals("9") && confirm == 0)
|
||||
{
|
||||
confirm = 1;
|
||||
fs.Position = position;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (jsonObject.ContainsKey("msg"))
|
||||
{
|
||||
returnMsg = jsonObject["msg"] + "";
|
||||
}
|
||||
|
||||
if (dataCode.Equals("-1"))
|
||||
{
|
||||
if (string.IsNullOrEmpty(returnMsg))
|
||||
{
|
||||
returnMsg = "上传失败,服务器未接收当前分片";
|
||||
}
|
||||
|
||||
hasFail = true;
|
||||
isUpload = false;
|
||||
break;
|
||||
}
|
||||
|
||||
failNum++;
|
||||
|
||||
if (failNum < 2 && returnMsg.IndexOf("权限") < 0 && returnMsg.IndexOf("禁止") < 0)
|
||||
{
|
||||
fs.Position = position;
|
||||
continue;
|
||||
}
|
||||
|
||||
hasFail = true;
|
||||
isUpload = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
returnMsg = $"上传请求失败,{result}";
|
||||
hasFail = true;
|
||||
isUpload = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasFail && position >= totalSize)
|
||||
{
|
||||
isUpload = true;
|
||||
|
||||
if (!string.IsNullOrEmpty(fileId))
|
||||
{
|
||||
BaseImpl.ExecSqlValue($"update p_fm_filetab set isdisabled = 2 where fileid = '{fileId}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user