using (var httpClient = new HttpClient())
{
string boundary = "----Boundary" + DateTime.Now.Ticks.ToString("x");
using (var multipartFormDataContent = new MultipartFormDataContent(boundary))
{
var values = new[]
{
new KeyValuePair<string, string>("id", snapOrRecords[i].fileId),
//new KeyValuePair<string, string>("file", "2"),
};
foreach (var keyValuePair in values)
{
multipartFormDataContent.Add(new StringContent(keyValuePair.Value),
String.Format("\"{0}\"", keyValuePair.Key));
}
multipartFormDataContent.Add(
new ByteArrayContent(System.IO.File.ReadAllBytes(@".\108摄像机_0_1698051272697.png")),
"file",
"108摄像机_0_1698051272697.png");
multipartFormDataContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=" + boundary);
var requestUri = "http://192.168.1.34:12001/sms/file/uploadById";
var html = httpClient.PostAsync(requestUri, multipartFormDataContent).Result.Content.ReadAsStringAsync().Result;
}
}
C# 使用HttpClient 上传文件
于 2023-10-24 09:28:09 首次发布