我正在编写 WCF Rest 服务。他将用于接收和保存图片。我使用 Windows Phone 7 客户端拍摄并发送照片。我可以发送在流上解析的图片(在服务端),但由于“参数错误”,图片不会被保存。
用于拍照的代码:
private void TakePictureAction()
{
//Show the camera
_camera.Show();
//Append when camera task finished.
_camera.Completed += delegate(object sender, PhotoResult e)
{
_photoImage = new BitmapImage();
_photoImage.SetSource(e.ChosenPhoto);
};
}
用于发送图片的代码(带休息):
var client = new RestClient("http://127.0.0.1:81/Service.svc");
var request = new RestRequest("/uploadImage", Method.POST);
request.AddBody(_photoImage);
try
{
client.ExecuteAsync(request, response =>
{
//Do stuff
});
}
catch (Exception ex)
{
MessageBox.Show("error");
}
和服务器端的代码:
public void UploadImage(Stream fileStream)
{
if (fileStream != null)
{
string filePath = @"c:\";
using (FileStream fileToUpload = new FileStream(filePath + "ok.bmp", FileMode.Create))
{
byte[] byteArray = new byte[10000];
int bytesRead = 0;
do
{
bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
if (bytesRead > 0)
{
fileToUpload.Write(byteArray, 0, bytesRead);
}
} while (bytesRead > 0);
using (MemoryStream ms = new MemoryStream(byteArray))
{
Bitmap img = (Bitmap)Image.FromStream(ms);
img.Save(@"c:\img.bmp");
}
}
}
}
我的服务方法
[OperationContract]
[WebInvoke(UriTemplate = "/uploadImage", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
void UploadImage(Stream img);
我哪里错了?