• HttpClient 上传文件


    /// <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 + " " + " ", "upload");
    }
    return data;
    }

  • 相关阅读:
    Java finally语句到底是在return之前还是之后执行(JVM字节码分析及内部体系结构)?
    关于git的文件内容冲突解决
    linux快速搭建
    2019年开始
    如何生成git ssh key
    2018年6月,最新php工程师面试总结
    引用变量 php面试总结1
    windows通过node环境搭建安装npm,cnpm,vue-cli
    回归程序世界,努力做出成绩
    我是谁?我在哪里?
  • 原文地址:https://www.cnblogs.com/FoR_Oscar/p/11867076.html
Copyright © 2020-2023  润新知