码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient 上传文件

时间:2019-11-15 15:50:11      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:上传   setting   new   except   tee   post   das   set   ada   

/// <summary>
/// 发送post请求
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="pID">患者ID</param>
/// <returns></returns>
public static string HttpPostRequst(string filePath, string pID)
{
string data = string.Empty;
try
{
string url = ConfigurationManager.AppSettings["WebURLPOST"];
var fileName = Path.GetFileName(filePath);
using (var fClient = new HttpClient())
{
MultipartFormDataContent content = new MultipartFormDataContent();

FileStream fileStream = File.Open(filePath, FileMode.Open);
StreamContent streamContent = new StreamContent(fileStream);
streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
streamContent.Headers.ContentDisposition.Name = "ecg_file";
streamContent.Headers.ContentDisposition.FileName = fileName;
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

HttpContent strParam = new StringContent("891164e528c4450485f356d662466e5c");
strParam.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
strParam.Headers.ContentDisposition.Name = "exam_id";

content.Add(strParam);
content.Add(streamContent);

data = fClient.PostAsync(new Uri(url), content).Result.Content.ReadAsStringAsync().Result;
streamContent.Dispose();
fileStream.Dispose();

}
}
catch (Exception ex)
{
CommonLog.WriteECGLog("发送文件流出错:" + ex.Message + "\r\n" + "\r\n", "upload");
}
return data;
}

HttpClient 上传文件

标签:上传   setting   new   except   tee   post   das   set   ada   

原文地址:https://www.cnblogs.com/FoR_Oscar/p/11867076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!