發表文章

目前顯示的是 10月, 2014的文章

如何讓Restful Service回應圖片

在一般利用WCF還做restful的服務,回應的部分可以指定要輸出為XML格式或是JSON格式,那麼如果說想要直接回應一張圖片的時候應該要怎麼進行呢? 手上是用WPF的應用程式,先Self Host一個restful service上去,之後來處理回應圖片的部分,下面是參考資訊來源 Self host http://stackoverflow.com/questions/20805317/how-to-set-up-wcf-self-hosted-rest-service Response image http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/fetching-image-using-wcf-rest-service/ http://stackoverflow.com/questions/12781173/cant-return-image-from-wcf-rest-service 主要的方式便是將圖片轉換成Stream,之後http這邊設定回應的類型是image,這樣就可以達成想要的樣子了,下面來看看程式碼的部分吧 Interface     [ ServiceContract ] interface   IMyService {     [ OperationContract ]     [ WebInvoke (UriTemplate  =   "/GetImage" , Method  =   "GET" ,         RequestFormat  =   WebMessageFormat . Xml, ResponseFormat  =   WebMessageFormat . Xml,         BodyStyle  =   WebMessageBodyStyle . Bare)]      Stream  GetImage(); } 很簡單的只有一個GetImage的Method 主程式這邊在Loaded事件中,去執行Self Host的相關動作 private   void  StartHost() {      Uri  httpUrl  =   new   Uri ( string . Fo