gpt4 book ai didi

c# - 使用 RestSharp 将图像上传到 WCF Api - 图像无效

转载 作者:行者123 更新时间:2023-11-30 16:25:00 26 4
gpt4 key购买 nike

我已经成功创建了一个应用程序,该应用程序将请求从我的 Windows Phone 发送到 Azure Web 角色中托管的 WCF api。我正在使用 RestSharp 来使用 Restful POST 方法来执行此操作。一切都运行良好,图像出现在存储中,但图像没有作为普通图像文件打开。

将它们与我之前上传的其他图像进行比较,在元数据信息中,它说无法查看的图像有一个 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 模拟器,它们应该都是相同的白色页面,角落里有一个小黑色方 block - 但文件之间的大小不同(有效图像文件的长度为 5670 ,原始代码的长度为 6076,使用上面第二个 addFile 的长度为 6239)

编辑#2进行更多分析,当图像流发送之前,其长度属性为 6116,当它到达服务器时,它的长度属性为 6370。我相信,从 RestSharp 方法中的某处添加 264 或者当数据流为在服务器端解释。 WCF服务的代码:

  [WebInvoke(UriTemplate = "/agerecog/{imageName}", Method = "POST")]
VizageResult analyseFace(string imageName, Stream imageStream);

最佳答案

问题已解决

我终于解决了这个问题,最终结果是可见的图像。发生的情况是 imageStream 还包含有关文件名的数据,这导致它(保存在 Azure 中时)不显示为图像文件。

我通过使用此处找到的 MultipartParser 类修复了此问题:http://multipartparser.codeplex.com/在服务器端将请求解析为文件名和文件流 block 。然后我使用正常过程保存了该 byte[] 中的图像。

我将网络请求数据解析为其组成部分的代码如下:

    MemoryStream imageStream = new MemoryStream();

MultipartParser parser = new MultipartParser(dataStream);
if (parser != null && parser.Success)
{
imageName = parser.Filename;
imageStream.Write(parser.FileContents, 0, parser.FileContents.Length);
}

当你使用内存流时,记得将其位置设置回 0 - 我犯过的一个小学生错误!

关于c# - 使用 RestSharp 将图像上传到 WCF Api - 图像无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10308015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com