HttpClient 上传文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

HttpClient 上传文件

as.秋意浓   2019-11-15 我要评论

/// <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;
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们