winform 通过webservice向服务器提交图片需要注意的地方

最近一个winform项目中需要通过拍照或者上传本地文件或者截图的方式把产品图片上传到服务器,最后选择了服务器部署webservice的方式来进行。其中遇到了一些问题记录下来。

不多说,直接上服务端代码

 [WebMethod(Description = "上传文件")]



    public bool UploadFiles(string filename, byte[] content)

    {

        try

        {

            int index = filename.LastIndexOf(".");

            if (index == 0)

            {

                return false;

            }

            else

            {

                string extended = string.Empty;

                if (index + 1 == filename.Length)

                {

                    return false;

                }

                else

                {

                    extended = filename.Substring(index + 1);

                    if (extended == "jpeg" || extended == "gif" || extended == "jpg" || extended == "png")

                    {

                        try

                        {

                            string Path = System.Web.HttpContext.Current.Server.MapPath("~/ProductImages/");

                            if (!Directory.Exists(Path)) 

                            {

                                 Directory.CreateDirectory(Path);

                            }

                         

                            MemoryStream mymemorystream = new MemoryStream(content, 0, content.Length);

                      

                            File.WriteAllBytes((Path + filename), content);

                            return true;

                        }

                        catch (Exception ex)

                        {

                            return false;

                        }

                    }

                    else

                    {

                        return false;

                    }

                }

            }

        }

        catch

        {

            return false;

        }

    }

  这其实是一种比较通用的方式,不仅可以用来接收图片文件,也可以是其他文件。当然你也可以做一些文件大小的限制,自己添加一个判断就行。没啥好说的,问题也没有出现。

接下来说说winform这边,下图是图片上传部分

winform 通过webservice向服务器提交图片需要注意的地方winform 通过webservice向服务器提交图片需要注意的地方

至于如何通过拍照和加载图片或者截图上传到picturebox上大家自己去找吧,网上一大堆。

接下来就是把picture的图片上传到服务器了,首先是添加服务引用,这也很简单就不说了,注意如果一个解决方案中有很多项目,而这个图片上传所在的项目不是主项目,那么需要在主项目的app.config文件中添加一个节点,否则会报找不到什么节点的错误。

<system.serviceModel>

<bindings>

<basicHttpBinding>

<binding name="WebServiceSoap" />

</basicHttpBinding>

</bindings>

<client>

<endpoint address="http://localhost/WebService.asmx" binding="basicHttpBinding"

bindingConfiguration="WebServiceSoap" contract="WebService.WebServiceSoap"

name="WebServiceSoap" />

</client>

</system.serviceModel>

  接下来就是上传部分了

  if (image != null&isnewimage)

            {

                MemoryStream ms = new MemoryStream();

                image.Save(ms, ImageFormat.Png);

                byte[] bytes = new byte[ms.Length];

                bytes = ms.GetBuffer();        

                WebService.WebServiceSoapClient webservice = new WebService.WebServiceSoapClient();

                string filename = cInvCode + ".png";

                if (webservice.UploadFiles(filename, bytes))

                {

                   

                }

                else

                {

                    issaveok = false;

                    failreason = "图片上传失败!";

                    

                    return;

                }             

            }

  这里首先获取图片资源放到一个image对象中,然后转换为字节数组通过webservice上传,这里我让图片全部以png格式上传,当然你可以以其他格式上传。

刚开始在向字节数组赋值的时候我用的不是bytes = ms.GetBuffer();而是用的ms.Write(bytes, 0, (int)ms.Length);结果文件是可以上传到服务器,但是服务器端的图片文件始终打不开,说可能文件已经损坏,查了半天查不出来原因,最后发现其实还有bytes = ms.GetBuffer();这种方法,一试,问题果然解决,服务器端的图片成为可以预览查看的正常图片了。

好了,这是第一次写博客,写的不好,还请吐槽啊。

你可能感兴趣的:(webservice)