我已成功制作了一个应用程序,该应用程序将来自我的 Windows Phone 的请求发送到托管在 azure Web 角色中的 WCF api。我正在使用 Restful POST 方法使用 RestSharp 来执行此操作。这一切都很好,图像出现在存储中,但图像没有像普通图像文件一样打开。
将它们与我之前上传的其他图像进行比较,在元数据信息中它说不可见的图像有一个 contentMD5 字段,该字段设置为“AKEYWqGgulwi6/9/VY2KPg==”(而其他人没有)这可能是导致的文件的问题?
我附上了我的 RestSharp 代码,也许我正在添加一些我不应该添加的东西?我怀疑的错误一定来自这里,因为这是操纵图像流的唯一地方。
public void SendRequest(Stream imageStream, string imageID)
{
var client = new RestClient();
client.BaseUrl = apiAddress;
var request = new RestRequest("agerecog/{imageName}", Method.POST);
request.AddUrlSegment("imageName", imageID);
//convert imagestream to byte array
var byteArray = new byte[imageStream.Length];
imageStream.Read(byteArray, 0, (int)imageStream.Length);
//add byteArray to request
request.AddFile("image/jpeg", byteArray, imageID);
var url = client.BuildUri(request).ToString();
client.ExecuteAsync<VizageResponse>(request, response =>
{
//request info. to be added here
});
}
编辑#1 经过一番工作,我决定将 addFile 行更改为:
request.AddFile(null, byteArray, null);
这改变了流的长度,也使 contentMD5 字段为空。但是,该图像仍然不是有效的图像文件。鉴于我要比较的图像都来自 Windows Phone 模拟器,它们都应该是相同的白页,角落有一个黑色的小方块 - 但文件之间的大小不同(有效图像文件的长度为 5670 ,原始代码长度为 6076,使用上面的第二个 addFile 长度为 6239)
编辑#2 做更多的分析,当图像流被发送之前,它的长度属性是 6116,当它到达服务器时,它是 6370。264 是从我相信的 RestSharp 方法中的某个地方添加的,或者当流数据在服务器端进行解释。WCF服务的代码:
[WebInvoke(UriTemplate = "/agerecog/{imageName}", Method = "POST")]
VizageResult analyseFace(string imageName, Stream imageStream);