4
我在通過RestSharp將圖像上傳到我的服務器時遇到了一些問題。RestSharp post image到WCF REST服務
我有一個Rest Wcf服務,接受一個流。如果我使用下面的代碼,我總是得到這樣的異常:
ProtocolViolationException字節寫入到流超過 指定的內容長度的字節大小。
什麼設置,我需要配置...設置內容長度頭似乎沒有什麼區別。
服務器端不接收圖像,但有一些較小的字節流。
任何幫助表示讚賞。
客戶端(檢驗)編碼:
byte[] byteArray = File.ReadAllBytes("small.jpg");
request.AddHeader("Content-Length", int.MaxValue.ToString());//doesn't matter what length I put here
request.AddFile("image/jpeg", (requestStream) =>
{
using (var ms = new MemoryStream(byteArray))
{
ms.CopyTo(requestStream, byteArray.Length);//doesn't matter whether I add second param or not
ms.Flush();
ms.Close();
}
},
"sample",
"image/jpeg");
request.Method = Method.POST;
client.ExecuteAsync(request, (response, a) =>
{
Assert.IsNotNull(response.Content);
string content = response.Content;
resetEvent.Set();
});
服務碼(返回所存儲的圖像的URL)
[OperationContract]
[WebInvoke(UriTemplate = "upload/{fileName}/{fileExtension}/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
Message UploadPhoto(string fileName, String fileExtension, string id, Stream fileContents);
嗯..所以也許文件太大了? – 2012-02-17 01:31:17
您應該只使用接受字節數組的「AddFile()'重載。 – 2012-02-17 01:34:22
*「當您決定哪個答案對您最有幫助時,請通過點擊答案左側的複選框大綱將其標記爲接受的答案。」*(來自stackoverflow.com/faq#howtoask)。請標記答案,如果它解決了你的問題! – DIF 2012-02-28 09:29:24