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();這種方法,一試,問題果然解決,服務器端的圖片成為可以預覽查看的正常圖片了。

好了,這是第一次寫博客,寫的不好,還請吐槽啊。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM