2008-09-19 74 views
1

什麼是創建一個用於接收圖像的web服務的最佳方式。 圖像可能相當大,我不想更改Web應用程序的默認接收大小。 我已經寫了一個接受二進制圖像,但我覺得必須有一個更好的選擇。WCF服務接收圖像

+0

圖像駐留在客戶端和Web服務器是不是能夠通過URI來獲取圖像。 – elgrego 2008-09-21 19:02:10

回答

4

哪裏這個形象「生活?」它可以在本地文件系統或網絡上訪問嗎?如果是的話,我建議讓您的WebService的接受URI(可以是URL或本地文件),並打開它作爲一個流,然後使用一個StreamReader讀取其中的內容。

例(但包裹在FaultExceptions例外,並添加FaultContractAttributes):

using System.Drawing; 
using System.IO; 
using System.Net; 
using System.Net.Sockets; 

[OperationContract] 
public void FetchImage(Uri url) 
{ 
    // Validate url 

    if (url == null) 
    { 
     throw new ArgumentNullException(url); 
    } 

    // If the service doesn't know how to resolve relative URI paths 

    /*if (!uri.IsAbsoluteUri) 
    { 
     throw new ArgumentException("Must be absolute.", url); 
    }*/ 

    // Download and load the image 

    Image image = new Func<Bitmap>(() => 
    { 
     try 
     { 
      using (WebClient downloader = new WebClient()) 
      { 
       return new Bitmap(downloader.OpenRead(url)); 
      } 
     } 
     catch (ArgumentException exception) 
     { 
      throw new ResourceNotImageException(url, exception); 
     } 
     catch (WebException exception) 
     { 
      throw new ImageDownloadFailedException(url, exception); 
     } 

     // IOException and SocketException are not wrapped by WebException :(   

     catch (IOException exception) 
     { 
      throw new ImageDownloadFailedException(url, exception); 
     } 
     catch (SocketException exception) 
     { 
      throw new ImageDownloadFailedException(url, exception); 
     } 
    })(); 

    // Do something with image 

} 
0

你不能使用FTP上傳圖像到服務器,那麼當這樣做服務器(因此WCF服務)麪包車輕鬆訪問它?你不需要與接收大小設置等方面思想家這樣

至少,這就是我做的。