SVN r1160
SVN-Revision: r1160
This commit is contained in:
@@ -170,6 +170,75 @@ namespace Lskj.Util
|
|||||||
return httpWebResponse;
|
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>
|
/// <summary>
|
||||||
/// url转换
|
/// url转换
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -107,6 +107,158 @@ namespace Lskj.Util
|
|||||||
}
|
}
|
||||||
return isUpload;
|
return isUpload;
|
||||||
}
|
}
|
||||||
|
public static bool UploadFile(string token, string postUrl, string filePath, string moduleCode, string idValue, string speciesno, out string fileId, out string rtnMsg)
|
||||||
|
{
|
||||||
|
bool isUpload = false;
|
||||||
|
fileId = "";
|
||||||
|
rtnMsg = "";
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileInfo fileInfo = new FileInfo(filePath);
|
||||||
|
|
||||||
|
int pageSize = 1 * 1024 * 1024;
|
||||||
|
long totalSize = fileInfo.Length;
|
||||||
|
long position = 0;
|
||||||
|
int comfirm = 0;
|
||||||
|
int encode = 4;
|
||||||
|
int failNum = 0;
|
||||||
|
bool hasFail = false;
|
||||||
|
|
||||||
|
string uploadToken = Guid.NewGuid().ToString("N").Substring(0, 24);
|
||||||
|
string uploadFileName = Path.GetFileName(filePath);
|
||||||
|
|
||||||
|
// 如果你后台没有 DoWebUploadSafe,就改回 DoWebUpload
|
||||||
|
string uploadMethod = "DoWebUpload";
|
||||||
|
|
||||||
|
Dictionary<string, string> headerDic = new Dictionary<string, string>();
|
||||||
|
headerDic.Add("Authorization", "Bearer " + token);
|
||||||
|
|
||||||
|
using (FileStream fs = new FileStream(filePath, 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 = GetUnixTime();
|
||||||
|
|
||||||
|
string uploadUrl = string.Format(
|
||||||
|
postUrl,
|
||||||
|
moduleCode,
|
||||||
|
HttpUtility.UrlEncode(idValue),
|
||||||
|
"file",
|
||||||
|
totalSize,
|
||||||
|
position,
|
||||||
|
HttpUtility.UrlEncode(uploadFileName),
|
||||||
|
uploadMethod,
|
||||||
|
HttpUtility.UrlEncode(speciesno),
|
||||||
|
comfirm,
|
||||||
|
encode,
|
||||||
|
uploadToken,
|
||||||
|
tsp
|
||||||
|
);
|
||||||
|
|
||||||
|
string result = "";
|
||||||
|
HttpWebResponse webResponse = HttpHelper.PostFileChunk(uploadUrl, buffer, uploadFileName, headerDic, out result);
|
||||||
|
|
||||||
|
if (webResponse != null && !string.IsNullOrEmpty(result))
|
||||||
|
{
|
||||||
|
JObject jsonObject = (JObject)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"] + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
position += readSize;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string dataCode = jsonObject.ContainsKey("data") ? jsonObject["data"] + "" : "";
|
||||||
|
|
||||||
|
if (dataCode.Equals("9") && comfirm == 0)
|
||||||
|
{
|
||||||
|
comfirm = 1;
|
||||||
|
fs.Position = position;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonObject.ContainsKey("msg"))
|
||||||
|
{
|
||||||
|
rtnMsg = jsonObject["msg"] + "";
|
||||||
|
}
|
||||||
|
|
||||||
|
failNum++;
|
||||||
|
|
||||||
|
if (failNum < 2 && rtnMsg.IndexOf("权限") < 0 && rtnMsg.IndexOf("禁止") < 0)
|
||||||
|
{
|
||||||
|
fs.Position = position;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
hasFail = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rtnMsg = "上传请求失败," + result;
|
||||||
|
hasFail = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasFail && position >= totalSize)
|
||||||
|
{
|
||||||
|
isUpload = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
rtnMsg = ex.Message;
|
||||||
|
isUpload = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isUpload;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long GetUnixTime()
|
||||||
|
{
|
||||||
|
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||||
|
return (long)(DateTime.UtcNow - startTime).TotalSeconds;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 上传文件
|
/// 上传文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user