fix: optimize large file chunk upload stability

This commit is contained in:
2026-08-06 18:02:11 +08:00
parent 031fc34f12
commit 219caba818
2 changed files with 134 additions and 68 deletions
+28 -14
View File
@@ -3911,8 +3911,6 @@ namespace Lskj.PubBomImport
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 code = "";
@@ -3925,7 +3923,7 @@ namespace Lskj.PubBomImport
//上传文件到服务器目录
//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}";
postUrl = "{0}Api/FileUploadApi.ashx?moduleId={1}&idValue={2}&speciesno={8}&folder={3}&totsize={4}&position={5}&filename={6}&uname=false&method={7}&comfirm={9}&relPath=&encode={10}&gzip=true&uploadToken={11}&__tsp={12}";
if (!string.IsNullOrEmpty(idValue))
{
@@ -3937,24 +3935,36 @@ namespace Lskj.PubBomImport
loginPmsDic.Add("username", System.Web.HttpUtility.UrlEncode(ERPInfo.Instance.UserName));
loginPmsDic.Add("password", System.Web.HttpUtility.UrlEncode(ERPInfo.Instance.InPassWord));
HttpWebResponse loginResponse = HttpHelper.Post(loginUrl, "", loginPmsDic, null, out string loginResult);
if (loginResponse != null && !string.IsNullOrEmpty(loginResult))
try
{
JObject jObject = JsonConvert.DeserializeObject<JObject>(loginResult);
if (jObject.ContainsKey("success"))
if (loginResponse != null && !string.IsNullOrEmpty(loginResult))
{
if ((jObject["success"] + "").Equals("True"))
JObject jObject = JsonConvert.DeserializeObject<JObject>(loginResult);
if (jObject.ContainsKey("success"))
{
if (jObject.ContainsKey("token"))
if ((jObject["success"] + "").Equals("True"))
{
token = jObject["token"] + "";
if (jObject.ContainsKey("token"))
{
token = jObject["token"] + "";
}
}
}
}
}
finally
{
loginResponse?.Close();
}
if (!string.IsNullOrEmpty(token))
{
int pageSize = 1 * 1024 * 1024;
long totalSize = fileInfo.Length;
// Web 前端上传器同样提示:经过防火墙的环境需要将分片
// 从 1MB 降到约 0.3MB。仅大文件启用安全分片,避免影响
// 现有小文件上传速度。
int pageSize = totalSize > 4L * 1024 * 1024
? 320 * 1024
: 1 * 1024 * 1024;
long position = 0;
int confirm = 1;
int encode = 4;
@@ -3963,7 +3973,7 @@ namespace Lskj.PubBomImport
string uploadToken = Guid.NewGuid().ToString("N").Substring(0, 24);
string uploadFileName = name;
string uploadMethod = "DoWebUpload";
string uploadMethod = "DoWebUploadSafe";
Dictionary<string, string> headerDic = new Dictionary<string, string>();
headerDic.Add("Authorization", $"Bearer {token}");
@@ -3993,7 +4003,7 @@ namespace Lskj.PubBomImport
string uploadUrl = string.Format(
postUrl,
SystemInfo.Instance.OAUrl,
"1000202",
"1000202-1",
System.Web.HttpUtility.UrlEncode(idValue),
"file",
totalSize,
@@ -4009,8 +4019,10 @@ namespace Lskj.PubBomImport
string result = "";
HttpWebResponse webResponse = HttpHelper.PostFileChunk(uploadUrl, buffer, uploadFileName, headerDic, out result);
bool hasResponse = webResponse != null;
webResponse?.Close();
if (webResponse != null && !string.IsNullOrEmpty(result))
if (hasResponse && !string.IsNullOrEmpty(result))
{
JObject jsonObject = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result);
string isSuccess = jsonObject["success"] + "";
@@ -4079,7 +4091,9 @@ namespace Lskj.PubBomImport
}
else
{
returnMsg = $"上传请求失败,{result}";
long chunkIndex = position / pageSize + 1;
long chunkCount = (totalSize + pageSize - 1) / pageSize;
returnMsg = $"上传请求失败(分片{chunkIndex}/{chunkCount},位置{position},分片大小{pageSize / 1024}KB),{result}";
hasFail = true;
isUpload = false;
break;