Skip to content

Commit

Permalink
[Alipay] 修复DoPostAsync 文件上传(未测试)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roc committed Jan 11, 2019
1 parent aace6a7 commit df83b42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/Essensoft.AspNetCore.Payment.Alipay/Utility/FileItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public FileItem(FileInfo fileInfo)
{
throw new ArgumentException("fileInfo is null or not exists!");
}

this.fileInfo = fileInfo;
}

Expand Down Expand Up @@ -82,6 +83,7 @@ public string GetFileName()
{
fileName = fileInfo.FullName;
}

return fileName;
}

Expand All @@ -91,6 +93,7 @@ public string GetMimeType()
{
mimeType = AlipayUtility.GetMimeType(GetContent());
}

return mimeType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -87,44 +87,31 @@ public static async Task<string> DoPostAsync(this HttpClient client, string url,
return await DoPostAsync(client, url, textParams);
}

var boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线

var reqStream = new MemoryStream();
var itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
var endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");

// 组装文本请求参数
var textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\nContent-Type:text/plain\r\n\r\n{1}";
foreach (var iter in textParams)
{
var textEntry = string.Format(textTemplate, iter.Key, iter.Value);
var itemBytes = Encoding.UTF8.GetBytes(textEntry);
reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
reqStream.Write(itemBytes, 0, itemBytes.Length);
}

// 组装文件请求参数
var fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
foreach (var iter in fileParams)
// 随机分隔线
var boundary = DateTime.Now.Ticks.ToString("X");
using (var requestContent = new MultipartFormDataContent(boundary))
{
var key = iter.Key;
var fileItem = iter.Value;
var fileEntry = string.Format(fileTemplate, key, fileItem.GetFileName(), fileItem.GetMimeType());
var itemBytes = Encoding.UTF8.GetBytes(fileEntry);
reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
reqStream.Write(itemBytes, 0, itemBytes.Length);
// 组装文本请求参数
foreach (var iter in textParams)
{
var streamContent = new StringContent(iter.Value, Encoding.UTF8, "text/plain");
requestContent.Add(streamContent, iter.Key);
}

var fileBytes = fileItem.GetContent();
reqStream.Write(fileBytes, 0, fileBytes.Length);
}
// 组装文件请求参数
foreach (var iter in fileParams)
{
var fileItem = iter.Value;
var byteArrayContent = new ByteArrayContent(fileItem.GetContent());
byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue(fileItem.GetMimeType());
requestContent.Add(byteArrayContent, iter.Key, fileItem.GetFileName());
}

reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);

using (var requestContent = new StringContent(reqStream.ToString(), Encoding.UTF8, "multipart/form-data;boundary=" + boundary))
using (var response = await client.PostAsync(url, requestContent))
using (var responseContent = response.Content)
{
return await responseContent.ReadAsStringAsync();
using (var response = await client.PostAsync(url, requestContent))
using (var responseContent = response.Content)
{
return await responseContent.ReadAsStringAsync();
}
}
}
}
Expand Down

0 comments on commit df83b42

Please sign in to comment.