HttpWebRequest 上傳圖片

來源:互聯網
上載者:User

標籤:對象   log   tin   code   add   guid   擷取   public   asp   

 public string HttpUploadFile()        {            string url = "http://localhost:50380/WebForm1.aspx";            string filepath = "C:\\Users\\lei2.wang\\Desktop\\Capture.PNG";            string fileformname = "Capture.PNG";            string poststr = "";            // 這個可以是改變的,也可以是下面這個固定的字串             string boundary = "------WebKitFormBoundarylAiKcuBRGd1pRVJI";            // 建立request對象             HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);            webrequest.ContentType = " multipart/form-data; boundary=----WebKitFormBoundarylAiKcuBRGd1pRVJI";            webrequest.Method = "POST";            webrequest.Headers.Add("Cookie: SGSRev_Order_ReportUser=billy-wp_li; SGSBlackClientListUser=seven_jiang; SGSCNComplaintCaseUser=Jiang_Seven; CtsisUserCode=apac\\Seven_Jiang; CNZZDATA1996164=cnzz_eid%3D798484094-1479962914-http%253A%252F%252Flocalhost%253A35708%252F%26ntime%3D1479974059 ");            webrequest.Referer = "http://localhost:50380/WebForm1.aspx";            //webrequest.Proxy = new WebProxy("127.0.0.1", 8888); // new WebProxy("127.0.0.1", "8888");//:8888            // 構造發送資料             StringBuilder sb = new StringBuilder();            // 文本域的資料,將user=eking&pass=123456  格式的文本域拆分 ,然後構造             sb.Append("\r\n");            sb.Append("\r\n");            sb.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI");            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"__VIEWSTATE\"");            sb.Append("\r\n");            sb.Append("\r\n");            sb.Append("revyQ+TvGvOWCVIQshKUEnHeGf0ftCH92iHWuRYVAct4lnXANDjVJUFuMPHDleYjIQfcrEsDhwzGYtRqw+fVLeUPs6OhI7/w2gubieoS4RQ=");            sb.Append("\r\n");            // 檔案域的資料             sb.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI");            sb.Append("\r\n");            sb.Append("Content-Disposition: form-data; name=\"aaab\"; filename=\"" + fileformname + "\"");            sb.Append("\r\n");            sb.Append("Content-Type: image/jpeg");            sb.Append("\r\n");            sb.Append("\r\n");            string postHeader = sb.ToString();            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);            //構造尾部資料             StringBuilder sb1 = new StringBuilder();            sb1.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI");            sb1.Append("\r\n");            sb1.Append("Content-Disposition: form-data; name=\"__VIEWSTATEGENERATOR\"");            sb1.Append("\r\n");            sb1.Append("\r\n");            sb1.Append("C687F31A");            sb1.Append("\r\n");            sb1.Append("------WebKitFormBoundarylAiKcuBRGd1pRVJI--");            sb1.Append("\r\n");            sb1.Append("\r\n");            byte[] boundaryBytes = Encoding.ASCII.GetBytes(sb1.ToString());            var cc = sb1.ToString();            FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);            long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;            webrequest.ContentLength = length;            Stream requestStream = webrequest.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);            WebResponse responce = webrequest.GetResponse();            Stream s = responce.GetResponseStream();            StreamReader sr = new StreamReader(s);            // 返回資料流(源碼)             return sr.ReadToEnd();        }

  

 protected void Page_Load(object sender, EventArgs e)        {             var aa=   Request.Files[0];                HttpFileCollection hfc = HttpContext.Current.Request.Files;  //擷取檔案,儲存圖片                HttpPostedFile hpf = hfc[0];                int extIndex = hpf.FileName.LastIndexOf(‘.‘);                string ext = hpf.FileName.Substring(extIndex);                string fileName = Guid.NewGuid().ToString();                string dir = HttpContext.Current.Server.MapPath("photos/buildings/");                hpf.SaveAs(dir + fileName + ext);        }

  

HttpWebRequest 上傳圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.