当前位置: 移动技术网 > IT编程>开发语言>c# > WebService 客户端上传图片,服务器端接收图片并保存到本地

WebService 客户端上传图片,服务器端接收图片并保存到本地

2020年04月28日  | 移动技术网IT编程  | 我要评论

需求:如题,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);

                }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网