WebService 客户端上传图片,服务器端接收图片并保存到本地
程序员文章站
2022-05-15 22:37:14
需求:如题,C#本地要调用Webservice接口,上传本地的照片到服务器中; 参考:客户端: https://blog.csdn.net/tiegenZ/article/details/79927670 服务端: https://www.cnblogs.com/zzzili/archive/201 ......
需求:如题,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:\users\administrator\desktop\2.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); }