[Asp.net] Using WebClient to upload images to remote services,

Source: Internet
Author: User

[Asp.net] Using WebClient to upload images to remote services,

I. Client

1. Page

1 <form id = "Form1" method = "post" runat = "server" enctype = "multipart/form-data"> 2 <input id = "MyFile" type = "file "runat =" server "/> 3 <br/> 4 <br/> 5 <asp: button ID = "Button1" runat = "server" Text = "Upload File" OnClick = "button#click"> </asp: Button> 6 </form>

 

2. Background

 1             System.Web.HttpFileCollection oFiles = System.Web.HttpContext.Current.Request.Files; 2             string FilePath = oFiles[0].FileName; 3             string FileName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1); 4             byte[] b = new byte[oFiles[0].ContentLength]; 5             System.IO.Stream fs = (System.IO.Stream)oFiles[0].InputStream; 6             fs.Read(b, 0, oFiles[0].ContentLength); 7             string postData = "data=" + HttpUtility.UrlEncode(Convert.ToBase64String(b)); 8             var webclient = new WebClient(); 9             webclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");10             byte[] byteArray = Encoding.UTF8.GetBytes(postData);11             //byte[] buffer = webclient.UploadData("http://localhost/datapush/DataPush.ashx", "POST", byteArray);//ashx12             byte[] buffer = webclient.UploadData("http://localhost/datapush/WebServiceDataPush.asmx/DataPush", "POST", byteArray);//asmx13             var msg = Encoding.UTF8.GetString(buffer);14             Response.Write(msg);

 

 

Ii. Server

1 string msg = ""; 2 byte [] filedata = Convert. fromBase64String (context. request ["data"]); 3 if (filedata. length = 0) 4 {5 msg = "{\" code \ ": \" uploaded empty file \ "}"; 6} 7 if (filedata. length> 1048576) 8 {9 msg = "{\" code \ ": \" the image size cannot exceed 1 MB \ "}"; 10} 11 string fileextension = filedata [0]. toString () + filedata [1]. toString (); 12 if (fileextension = "7173") 13 {14 fileextension = "gif"; 15} 16 else if (fileextension = = "255216") 17 {18 fileextension = "jpg"; 19} 20 else if (fileextension = "13780") 21 {22 fileextension = "png "; 23} 24 else if (fileextension = "6677") 25 {26 fileextension = "bmp"; 27} 28 else if (fileextension = "7373 ") 29 {30 fileextension = "tif"; 31} 32 else33 {34 msg = "{\" code \ ": \" the uploaded file is not an image \"}"; 35} 36 37 try38 {39 // Save the image 40 string filename = Guid. newGuid (). toString ("D") + ". "+ fileextension; 41 System. IO. MemoryStream MS = new System. IO. MemoryStream (filedata); 42 System. IO. FileStream fs = new System. IO. FileStream (context. Server. MapPath ("~ /") +"/Capture image/"+ filename, System. IO. fileMode. create); 43 ms. writeTo (fs); 44 ms. close (); 45 fs. close (); 46 fs = null; 47 MS = null; 48 msg = "{\" code \ ": \" uploaded image \"}"; 49} 50 catch (Exception exe) 51 {52 msg = "{\" code \ ": \" "+ exe. message + "\"} "; 53} 54

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.