MVC service-side code:
[HttpPost] PublicActionResult Uploadimg (stringtypes) {stringdata =""; Try { if(Types = ="Image") { foreach(stringFinchRequest.Files.AllKeys) {stringPATHT = HttpRuntime.AppDomainAppPath.ToString () +"/uploadimages/"; stringPATHD = DateTime.Now.ToString ("YYYYMMDD") +"/"+ DateTime.Now.ToString ("HHmm") +"/"; stringspath = patht +PATHD; if(!directory.exists (spath)) {directory.createdirectory (spath); } httppostedfilebase File=Request.files[f]; Random seed=NewRandom (); intRandomnum = seed. Next (Ten, About); stringFileName = DateTime.Now.ToString ("HHMMSS") + randomnum.tostring () +". jpg"; File. SaveAs (spath+fileName); stringImgstr = spath +FileName; Data="{\ "code\": \ "10000\", \ "message\": \ ""+ Imgstr +"\"}"; } } Else{Data="{\ "code\": \ " -10000\", \ "message\": \ "Incorrect upload format \"}"; } } Catch(Exception ex) {data="{\ "code\": \ " -10000\", \ "message\": \ ""+ ex. Message +"\"}"; } returnContent (data,"Application/json"); }
Public Partial classUpLoadImage:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {WebClient WebClient=NewWebClient (); stringFileName =@"C:\Users\admin\Desktop\tom2.jpg"; byte[] Responsearray = WebClient. UploadFile ("Http://localhost:8987/Home/UploadImg?types=image","POST", FileName); stringGetPath = encoding.getencoding ("UTF-8"). GetString (Responsearray); Response.Write (GetPath); } }
WebClient Uploading Files