Tom*_*mer 3 wcf file-upload image-uploading wcf-rest
作为一个非常大的应用程序的一部分,我需要提供一个上传图像的REST服务.由于整个应用程序都是.NET,我一直在尝试找到一个WCF RESTFul服务的工作示例,它可以做到但不能(定义整个配置).网上有一些例子但我不能(由于我在WCF中缺乏知识)使它们都不起作用.(我不断抛出异常).
我很乐意与工作人员联系起来.
请在下面找到示例:
在服务器上:
接口:
[WebInvoke]
string PostImage(Stream stream);
Run Code Online (Sandbox Code Playgroud)
我的实现类:
public string PostImage(Stream stream)
{
byte[] buffer = new byte[10000];
stream.Read(buffer, 0, 10000);
FileStream f = new FileStream("C:\\temp\\sample.jpg", FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
stream.Close();
return "Recieved the image on server";
}
Run Code Online (Sandbox Code Playgroud)
注意:确保您的WCF服务应用程序应用程序池标识具有对c:\ Temp位置的写访问权以写入该文件.或者,您可以替换逻辑以将映像保存到db或您希望的任何其他方式.
我在服务器上的web.config:
<services>
<service name="XMLService.Service1" behaviorConfiguration="default">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Service1" contract="XMLService.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
<serviceBehaviors>
<behavior name="default">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<webHttpBinding>
<binding name="RestBinding">
<readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" />
<security mode="None">
</security>
</binding>
</webHttpBinding>
Run Code Online (Sandbox Code Playgroud)
现在我的客户端上有以下代码:
var client = new RestClient();
client.BaseUrl = "http://localhost/XMLService/Service1.svc";
var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = resourceUrl };
if(requestBody !=null)
request.AddParameter("objCompositeType", requestBody);
request.AddFile("stream", image, "Array.png");
var response = client.Execute(request);
string responseString;
if (response.StatusCode == HttpStatusCode.OK)
{
responseString = HttpUtility.HtmlDecode(response.Content);
}
else
{
responseString = response.StatusDescription + " --------------------" + HttpUtility.HtmlDecode(response.Content);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用第三方工具RestSharp将我的请求发布到REST服务.
我在提琴手中的请求如下所示:
POST http://localhost/XMLService/Service1.svc/PostImage HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp 102.0.0.0
Host: rajeshwin7
Content-Type: multipart/form-data; boundary=-----------------------------28947758029299
Content-Length: 2300
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
-------------------------------28947758029299
Content-Disposition: form-data; name="stream"; filename="download.jpg"
Content-Type: application/octet-stream
?????JFIF??????????C?
!#'**%%+7,$3)1-,*4696A),,5???C
6$$555,,)404,)6,51,5),)+)),,0,.044,),1),)),)))42,,,0,???"??"??????????????????.???????!1"AQ2a?#q?B?Rr?????????????????????????????????1????????4?????????????????????OF??¼?(?%?az?ROw????l"???Z??g}J?qWR???R?K)?;???<&???G??kw???K???):1?????????`?\?B;e(?:u"????.???_??QS?iB)?2?w?{??Q???k?%?e?xU?^?!Bw~??b?)????-,???e??=k???s??v???}? ???$?}2}???o?>5N???????Y?L???T??)=??|?gu?B^??&?:st???J?8ae?/?O-%??I???
ES??}?$??MI7',nK8?xG???m??F????K?g)?n^=?#?1?????{P??
??U5)8?????8k??N?f????8??u?S?M9??I?K8M?xY???????????????j?EZ??[Y?O??(t??H?W?K2?~?M?cn?????M_?????&?K??g????????l;??}%A?if?f??Q?MB{1?xofs?u?sX?qsW???T??)V????'??<????S???Y?t
?=?i?R?:nJsm?U?T???-???,?6U??s??N????j?_?r?5O???8?%??????????????????V???N??as???>_<J??v?:p?B?O??A?&?q???>L}N???a{?????X?U???C???~?X?e?]???3NrWZ???M(F?kd%'??{0???4?????U}M]J????ue???ia%??????+K??n]??i???n????Mg?,????S??~?W??V?)m?95??Rm??q?????P?
m5_wjQ????m,.??Rry^??o??}?O?y^9?N??MT?G??2?9G???+>7=^?^??J4??1?%%???*YMK?Z?s?5 )??Y?U?y:pj?:*S?mU%?y???24??I)-?q?$?????C]?J?4?(????????d????g?fv?;R????yO?+??????????????????([Z?V?T?R?Jks?K???8?
?4?Wv6?m?6??K?y^y
??u??????E????????'E?P?u?????
M5??????8b?5{??V?IB2\6??o????O???<???GVR??g?7??,<???<??|???Q??T???CRx???>1??V???uj<?7'?r?O??????????????????)B??S?Y???????z?P?8W?$??n~?S?M??u?<????????????????????]????/?:j+n"???I???????,???'F?c?yt?%&?4??k9??0???????????????????????i?[FP???9?????7????,???h?S??_d??????????????????????%V?XSuj?1sQ????y?u??y??i?)?*?I??W??Y?
&???K?H ????????????????????K?q
zRV?8?#:p???krk>?>?-?='??{??U}?e?
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
-------------------------------28947758029299--
Run Code Online (Sandbox Code Playgroud)
当我执行上述请求的POST时,我得到以下响应:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 105
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=fdbg45tergdsfg434t; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 27 Feb 2012 12:05:32 GMT
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Recieved the image on server</string>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16735 次 |
| 最近记录: |