| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1677 人关注过本帖
标题:httpwebrequest post方式上传图片的问题
只看楼主 加入收藏
wanghaotong
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2009-3-6
结帖率:76.47%
收藏
 问题点数:0 回复次数:0 
httpwebrequest post方式上传图片的问题

程序功能 使用post数据的方式提交一些内容 其中就包括上传图片 程序没有报错 就是数据提交不了。贴上代码:希望高手指教或加我QQ 363455936 详谈
程序代码:
 ListViewItem item = this.Sendlist.Items[sendindex];
                string Category = item.SubItems[1].Text;
                string Title = item.SubItems[2].Text;
                string Content = item.SubItems[3].Text;
                string Pic = item.SubItems[4].Text;
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                WebClient client = new WebClient();
                string filepath = @"C:\Users\Administrator\Downloads\image.jpg";
                client.DownloadFile(Pic, filepath);
                //string boundary = "-----------------------------7db2be252903bc";
                string poststr = "category=" + Category + "&district=luwan_xintiandi&title=" + Title + "&description=<p>" + Content + "</p>";
                StringBuilder sb = new StringBuilder();
                foreach (string c in poststr.Split('&'))
                {
                    string[] itemdata = c.Split('=');
                    if (itemdata. Length != 2)
                    {
                        break;
                    }
                    string name = itemdata[0];
                    string value = itemdata[1];
                    sb.Append(boundary);
                    sb.Append("\r\n");
                    sb.Append("Content-Disposition: form-data; name=\"" + name + "\"");
                    sb.Append("\r\n\r\n");
                    sb.Append(value);
                    sb.Append("\r\n");
                }
                // 文件域的数据
                sb.Append(boundary);
                sb.Append("\r\n");
                sb.Append("Content-Disposition: form-data; name=\"image\";filename=\"" +filepath  + "\"");
                sb.Append("\r\n");
                sb.Append("Content-Type: ");
                sb.Append("image/x-png");
                sb.Append("\r\n\r\n");
                string postHeader = sb.ToString();
                //string postdata = boundary
                //            + "\r\nContent-Disposition: form-data; name=\"category\"\r\n"

                //            + "\r\n" + Category//分类
                //            + "\r\n" + boundary
                //            + "\r\nContent-Disposition: form-data; name=\"district\"\r\n"

                //            + "\r\nluwan_xintiandi"//二级分类
                //            + "\r\n"
                //            + "\r\nContent-Disposition: form-data; name=\"title\"\r\n"

                //            + "\r\n" + Title //标题
                //            + "\r\n-----------------------------7db2be252903bc"
                //            + "\r\nContent-Disposition: form-data; name=\"description\"\r\n"

                //            + "\r\n<p>" + Content + "</p>"//正文
                //            + "\r\n-----------------------------7db2be252903bc"
                //            + "\r\nContent-Disposition: form-data; name=\"image\"; filename=\"" + Pic + "\""//图片
                //            + "\r\nContent-Type: image/x-png\r\n"
                //            + "\r\n-----------------------------7db2be252903bc"
                //            + "\r\nContent-Disposition: form-data; name=\"commit\"\r\n"

                //            + "\r\nPost This Now"
                //            + "\r\n-----------------------------7db2be252903bc--\r\n";
                //byte[] data = Encoding.Default.GetBytes(postdata);
                HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create("http://");
                wrq.CookieContainer = cc;
                wrq.Method = "POST";
                wrq.Accept = "*/*";
                wrq.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Tablet PC 2.0)";
                wrq.ContentType = "multipart/form-data; boundary="+boundary;
                wrq.Referer = "http://";
                byte[] postHeaderBytes = Encoding.Default.GetBytes(postHeader);
                //构造尾部数据
                string enddata = "\r\n" + boundary
                            + "\r\nContent-Disposition: form-data; name=\"commit\"\r\n"
                            + "\r\nPost This Now"
                            + "\r\n" + boundary + "--\r\n";
                byte[] boundaryBytes = Encoding.Default.GetBytes(enddata);
             
                FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
                long leng = data.Length + fileStream.Length;
                wrq.ContentLength = leng;
                Stream requestStream = wrq.GetRequestStream();
                // 输入头部数据
                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                // 输入文件流数据
                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);
                //输入尾部数据
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                //requestStream.Write(data, 0, data.Length);
                HttpWebResponse wrp = (HttpWebResponse)wrq.GetResponse();

                string str = new StreamReader(wrp.GetResponseStream(), Encoding.Default).ReadToEnd();
                wrp.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("发送当前信息时出错,err:" + ex.ToString());
                return;
            }

搜索更多相关主题的帖子: 上传图片 color 
2011-06-03 13:34
快速回复:httpwebrequest post方式上传图片的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056432 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved