SVN r1160

SVN-Revision: r1160
This commit is contained in:
tdx
2026-04-23 09:09:45 +00:00
parent 951e59ea26
commit 809d3df242
2 changed files with 221 additions and 0 deletions
+69
View File
@@ -170,6 +170,75 @@ namespace Lskj.Util
return httpWebResponse;
}
}
/// <summary>
/// 分片上传
/// </summary>
/// <param name="url"></param>
/// <param name="fileBytes"></param>
/// <param name="fileName"></param>
/// <param name="headerDic"></param>
/// <param name="result"></param>
/// <returns></returns>
public static HttpWebResponse PostFileChunk(string url, byte[] fileBytes, string fileName, Dictionary<string, string> headerDic, out string result)
{
HttpWebResponse httpWebResponse = null;
try
{
string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.Method = "POST";
httpWebRequest.Accept = Accept;
httpWebRequest.UserAgent = UserAgent;
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
if (headerDic != null)
{
foreach (var item in headerDic)
{
if (!item.Key.Equals("Content-Type"))
{
httpWebRequest.Headers.Add(item.Key, item.Value);
}
}
}
string header =
"--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
string footer = "\r\n--" + boundary + "--\r\n";
byte[] headerBytes = Encoding.UTF8.GetBytes(header);
byte[] footerBytes = Encoding.UTF8.GetBytes(footer);
httpWebRequest.ContentLength = headerBytes.Length + fileBytes.Length + footerBytes.Length;
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(headerBytes, 0, headerBytes.Length);
stream.Write(fileBytes, 0, fileBytes.Length);
stream.Write(footerBytes, 0, footerBytes.Length);
stream.Close();
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
result = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return httpWebResponse;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
result = ex.Message;
return httpWebResponse;
}
}
/// <summary>
/// url转换
/// </summary>