需求:如题,C#本地要调用Webservice接口,上传本地的照片到服务器中;
参考:客户端: https://blog.csdn.net/tiegenZ/article/details/79927670
服务端: https://www.cnblogs.com/zzzili/archive/2012/12/16/6662668.html
服务端接收的图片是base64编码的字节流:
[WebMethod(Description = "上传图片")] public string getImageByte(Byte[] getByte) { string savaImageName = null; try { DateTime dt = DateTime.Now; string sFile = dt.ToShortDateString().ToString();//2005/11/5 String file = "/images/" + sFile;// /images/2005/11/5 if (Directory.Exists(Server.MapPath(file)) == false)//如果文件不存在 则创建 { Directory.CreateDirectory(Server.MapPath(file)); } savaImageName = file + "/" + dt.ToFileTime().ToString() + ".png";//127756416859912816 FileStream fs = new FileStream(Server.MapPath(savaImageName), FileMode.Create, FileAccess.Write); fs.Write(getByte, 0, getByte.Length); fs.Flush(); fs.Close(); } catch (Exception e) { } return savaImageName; }
客户端直接添加服务引用,调用相关方法:
string strFilePath = @"C:UsersAdministratorDesktop2.jpg"; FileInfo fi = new FileInfo(strFilePath); if (File.Exists(strFilePath)) { byte[] b = new byte[fi.Length]; System.IO.FileStream fs = fi.OpenRead(); fs.Read(b, 0, Convert.ToInt32(fi.Length)); WebReference.AppWebService ss = new WebReference.AppWebService(); string sr= ss.getImageByte(b); }