HttpWebResponse/HttpWebRequest上传图片 [英] HttpWebResponse/HttpWebRequest upload image

查看:75
本文介绍了HttpWebResponse/HttpWebRequest上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将映像加载到服务器的问题
我尝试同时上传图片并发送数据(POST)方法

我使用此代码

i have prolem with uloadinge image to server
i try to upload image and send data (POST) method a the same time

i use this code

HttpWebResponse response;                Stream resStream;                StreamReader objSR;                CookieContainer cookieContainer = new CookieContainer();                ASCIIEncoding encoding = new ASCIIEncoding();                HttpWebRequest request;                string sResponse;                string postData;                byte[]  data;                string UrlLogin;                
//Upload Image Page **************************************                
//**************************************************************
                UrlLogin = "http://localhost:63637/whois/test0.aspx";                string uploadfile = "C:\\fax\\pics\\7130570.jpg";                string fileFormName = "userfile";                string contenttype = "image/pjpeg";                postData = "?wm_flag=" + "0";                postData += ("&action=" + "process");                postData += ("&hi=" + "ft");                request = (HttpWebRequest)WebRequest.Create(UrlLogin + postData);                //request.Accept = "*/*";                request.KeepAlive = true;                request.CookieContainer = cookieContainer;                string boundary = "----------" + DateTime.Now.Ticks.ToString("x");                request.ContentType = "multipart/form-data; boundary=" + boundary;                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";                request.Method = "POST";                request.AllowAutoRedirect = true;                data = encoding.GetBytes(postData);                StringBuilder sb = new StringBuilder();                sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(fileFormName); sb.Append("\"; filename=\"");                sb.Append(Path.GetFileName(uploadfile)); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(contenttype); 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(uploadfile, FileMode.Open, FileAccess.Read);                long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;// +data.Length;                //postData += ("&userfile=" + "C:\\Users\\tareq\\Desktop\\111111.jpg");                                request.ContentLength = length;//data.Length;                Stream requestStream = request.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);                response = (HttpWebResponse)request.GetResponse();                resStream = response.GetResponseStream();                objSR = new StreamReader(resStream);                sResponse = objSR.ReadToEnd();




此代码可以正常工作,上传照片没有任何问题
但是我在服务器中的request.form ["wm_flag"]问题出现了错误,因为我必须以POST的方式发送它,而且我不知道该怎么做?请帮助...




this code workfinewithout any problem with uploading photo
but my problem with i request.form["wm_flag"] in the server i got error because i must send it as POST and i don''t know how to do it ?? please HELP ...

推荐答案

我通过添加
找到了解决方案

i found the solution by adding


sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", p.Name));
sb.AppendLine();
sb.AppendLine(p.Value);


这篇关于HttpWebResponse/HttpWebRequest上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆