寒天生活

寒天点滴
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c# 模拟表单提交 有附件

Posted on 2009-09-14 22:18  寒天  阅读(2364)  评论(1)    收藏  举报
最近项目需要,需要在winform中模拟表单将数据提交至服务器,发现单独提交键值对很容易实现,单独实现上传文件也很容易实现。要是同时提交键值对和文件,比较麻烦。在百度谷歌了大半天没有任何收获。无奈之下,按照 黑月.Net的 的思路去自己写。经过奋斗 终于搞定。
方法如下:


        public WebResponse SubmitData(string fileName, Uri uri, string[] keys, string[] values)
        {
            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
            //httpWebRequest2.Credentials = credentialCache;//
            httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
            httpWebRequest2.Method = "POST";

            // Build up the post message header

            StringBuilder sb = new StringBuilder();

            if (keys != null)
            {
                for (int i = 0; i < keys.Length; i++)
                {
                    sb.Append("--");
                    sb.Append(boundary);
                    sb.Append("\r\n");
                    sb.Append("Content-Disposition: form-data; name=\"" + keys[i] + "\"\r\n\r\n");
                    sb.Append(values[i]);
                    sb.Append("\r\n");
                }
            }


            sb.Append("--");
            sb.Append(boundary);
            sb.Append("\r\n");
            sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
            sb.Append(Path.GetFileName(fileName));
            sb.Append("\"");
            sb.Append("\r\n");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");

            string postHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);

            // Build the trailing boundary string as a byte array
            // ensuring the boundary appears on a line by itself
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
            httpWebRequest2.ContentLength = length;

            Stream requestStream = httpWebRequest2.GetRequestStream();

            // Write out our post header
            requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

            // Write out the file contents
            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                requestStream.Write(buffer, 0, bytesRead);

            // Write out the trailing boundary
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);

            WebResponse webResponse2 = httpWebRequest2.GetResponse();

            requestStream.Close();
            fileStream.Close();
            return webResponse2;

        }


该方法也是在百度谷歌到的,经过简单的修改而成。这个方法只能提交一个文件,多个文件简单修改下就OK了。最后别忘了关闭webResponse2哦。

原方法地址:http://topic.csdn.net/u/20081002/19/33542187-4c1e-49ed-86f8-439240384b79.html
黑月.Net地址:http://www.cnblogs.com/dxxhh/archive/2007/11/16/961897.html