zoukankan      html  css  js  c++  java
  • winform HttpWebRequest上传文件

    Winform点击button按钮上传:

    string filePath = "E:\test.rar";
                string fileName = "test.rar";
                string postURL = "http://localhost:5995/Default.aspx";
    
                // 边界符  
                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();
    
                // Key-Value数据  
                var stringKeyHeader = "
    --" + boundary +
                                       "
    Content-Disposition: form-data; name="{0}"" +
                                       "
    
    {1}
    ";
    
                Dictionary<string, string> stringDict = new Dictionary<string, string>();
                stringDict.Add("len", "500");
                stringDict.Add("wid", "300");
                foreach (byte[] formitembytes in from string key in stringDict.Keys
                                                 select string.Format(stringKeyHeader, key, stringDict[key])
                                                     into formitem
                                                     select Encoding.UTF8.GetBytes(formitem))
                {
                    memStream.Write(formitembytes, 0, formitembytes.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并设置属性  
                var webRequest = (HttpWebRequest)WebRequest.Create(postURL);
                webRequest.Method = "POST";
                webRequest.Timeout = 100000;
                webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
                webRequest.ContentLength = tempBuffer.Length;
    
                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();

    Web站点Default.aspx页面:

      Response.ContentType = "text/plain";
            if (Request.Files.Count == 0)
            {
                Response.Write("No file");
                return;
            }
    
            HttpPostedFile f1 = Request.Files[0];
            f1.SaveAs("E:\test-upload.rar");

    string strPars = "";
    foreach (var key in Request.Form.AllKeys)
    {
     string val = Request[key];
     strPars += "[" + key + ":" + val + "] ";
    }
    image.Dispose();
    Response.Write("OK Get File:" + f1.FileName + " Pars:" + strPars);

    如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!
    版权声明:本文原创发表于 博客园,作者为 码农-小菜鸟 本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
  • 相关阅读:
    【hive】时间段为五分钟的统计
    【git】提交到github不显示贡献小绿点问题的解决
    【hive】关于用户留存率的计算
    【hive】求日期是星期几
    【hive】数据仓库层次设计
    【hive】count() count(if) count(distinct if) sum(if)的区别
    转载:几种 hive join 类型简介
    丑小鸭和白天鹅没有区别
    好好照顾自己,就像照顾你爱的人那样;
    灵光一闪(最近更新于2020/8/23)
  • 原文地址:https://www.cnblogs.com/xiaoxiaocainia/p/7356429.html
Copyright © 2011-2022 走看看