Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to upload an image to PHP server but still have problem
when I access url direct
http://images3.souq.com/upload_pict_url.phpi can send image
Please help!

C#
private void button1_Click(object sender, EventArgs e)
{
    //using System.IO;//using System.Net;
    HttpWebResponse response;
    Stream resStream;
    StreamReader objSR;
    CookieContainer cookieContainer = new CookieContainer();
    ASCIIEncoding encoding = new ASCIIEncoding();
    HttpWebRequest request;
    string sResponse;
    string postData;
    string UrlLogin;


        try
        {
            UrlLogin = "http://images3.souq.com/upload_pict_url.php";
            //UrlLogin = "http://localhost:63637/whois/test0.aspx";
            string uploadfile = "C:\\fax\\pics\\7130570.jpg";
            string fileFormName = "userfile";
            string contenttype = "image/jpeg";//"image/pjpeg";

            request = (HttpWebRequest)WebRequest.Create(UrlLogin);
            //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;

            StringBuilder sb = new StringBuilder();

            string name1 = "wm_flag";
            string value1 = "0";

            sb.Append("--");
            sb.AppendLine(boundary);
            sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", name1));
            sb.AppendLine();
            sb.AppendLine(value1);

            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;
            request.ContentLength = 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();
        }
        catch (Exception x) { }
}
private void button1_Click(object sender, EventArgs e)
{
    //using System.IO;//using System.Net;
    HttpWebResponse response;
    Stream resStream;
    StreamReader objSR;
    CookieContainer cookieContainer = new CookieContainer();
    ASCIIEncoding encoding = new ASCIIEncoding();
    HttpWebRequest request;
    string sResponse;
    string postData;
    string UrlLogin;


        try
        {
            UrlLogin = "http://images3.souq.com/upload_pict_url.php";
            //UrlLogin = "http://localhost:63637/whois/test0.aspx";
            string uploadfile = "C:\\fax\\pics\\7130570.jpg";
            string fileFormName = "userfile";
            string contenttype = "image/jpeg";//"image/pjpeg";

            request = (HttpWebRequest)WebRequest.Create(UrlLogin);
            //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;

            StringBuilder sb = new StringBuilder();

            string name1 = "wm_flag";
            string value1 = "0";

            sb.Append("--");
            sb.AppendLine(boundary);
            sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", name1));
            sb.AppendLine();
            sb.AppendLine(value1);

            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;
            request.ContentLength = 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();
        }
        catch (Exception x) { }
}
Posted
Updated 7-Nov-10 4:43am
v3
Comments
shakil0304003 9-Nov-10 10:07am    
Large code, no one will interest to read this :(
Sandeep Mewara 21-Nov-10 6:12am    
What error you get?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900