• winform上传文件,利用http,form-data格式上传


    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="url">服务地址</param>
    /// <param name="filePath">文件路径</param>
    public static string DoPostFile(string url, string filePath)
    {
    string fileName = Path.GetFileName(filePath);

    // 边界符
    var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
    var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + " ");
    var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

    // 最后的结束符
    var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "-- ");

    // 文件参数头
    const string filePartHeader =
    "Content-Disposition: form-data; name="{0}"; filename="{1}" " +
    "Content-Type: application/octet-stream ";
    var fileHeader = string.Format(filePartHeader, "file", fileName);
    var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);

    // 开始拼数据
    var memStream = new MemoryStream();
    memStream.Write(beginBoundary, 0, beginBoundary.Length);

    // 文件数据
    memStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
    var buffer = new byte[1024];
    int bytesRead; // =0
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    {
    memStream.Write(buffer, 0, bytesRead);
    }
    fileStream.Close();

    // 写入字符串
    var keyValue = "Content-Disposition: form-data; name="{0}" {1} ";
    Dictionary<string, string> stringDict = new Dictionary<string, string>();
    stringDict.Add("len", "500");
    stringDict.Add("wid", "300");
    foreach (string key in stringDict.Keys)
    {
    var keyValueBytes = Encoding.UTF8.GetBytes(string.Format(keyValue, key, stringDict[key]));
    memStream.Write(beginBoundary, 0, beginBoundary.Length);
    memStream.Write(keyValueBytes, 0, keyValueBytes.Length);
    }

    // 写入最后的结束边界符
    memStream.Write(endBoundary, 0, endBoundary.Length);

    //倒腾到tempBuffer
    memStream.Position = 0;
    var tempBuffer = new byte[memStream.Length];
    memStream.Read(tempBuffer, 0, tempBuffer.Length);
    memStream.Close();

    // 创建webRequest并设置属性
    HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
    //添加请求头
    AddHttpHeader(webRequest);
    webRequest.Method = "POST";
    webRequest.Timeout = 100000;
    webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
    webRequest.ContentLength = tempBuffer.Length;
    webRequest.KeepAlive = false;
    webRequest.ProtocolVersion = HttpVersion.Version10;

    var requestStream = webRequest.GetRequestStream();
    requestStream.Write(tempBuffer, 0, tempBuffer.Length);
    requestStream.Close();

    var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
    string responseContent;
    using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
    {
    responseContent = httpStreamReader.ReadToEnd();
    }

    httpWebResponse.Close();
    webRequest.Abort();
    return responseContent;
    }

  • 相关阅读:
    页面切换语言包使用session不用cookie
    如何设置unobtrusive的语言包
    .net MVC全球化资源使用心得
    消息队列写入内容后,读出来的自动包裹了<string>标签,自定义格式化器解决该issue
    解决"415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'"
    动态sql语句输出参数
    vue数据更改视图不更新问题----深入响应式原理
    实现 页面某些 效果
    自己封装 vue 组件 和 插件
    单页面应用的 打包部署(vue-cli、creat-react-app )
  • 原文地址:https://www.cnblogs.com/dachuang/p/10174592.html
Copyright © 2020-2023  润新知