zoukankan      html  css  js  c++  java
  • 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这边,下图是图片上传部分

    至于如何通过拍照和加载图片或者截图上传到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();这种方法,一试,问题果然解决,服务器端的图片成为可以预览查看的正常图片了。

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

  • 相关阅读:
    Docker初识
    57、android 应用内全局通知的实现方法
    56、使用android studio(v1.3.*)修改包名 (rename package name)
    55、android app借助友盟实现微信授权登录
    54、edittext输入类型限制为ip,inputType应该如何设置
    53、listview、expandableListview如何选中时保持高亮?
    52、sb犯的错误
    51、如何提取android代码中的字符串为系统资源文件 (I18N)
    50、转自知乎上android开发相见恨晚的接口
    49、android studio 使用技巧记录
  • 原文地址:https://www.cnblogs.com/srszzw/p/3302602.html
Copyright © 2011-2022 走看看