我正在尝试使用API上传训练图像。 https://southcentralus.dev.cognitive.microsoft.com/docs/services/d9a10a4a5f8549599f1ecafc435119fa/operations/58d5835bc8cb231380095be3
这是我的代码。
public async Task<CreateImageResult> CreateImagesFromData(byte[] _image)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Training-key", _trainingKey);
HttpResponseMessage response;
using (var content = new ByteArrayContent(_image))
{
response = await client.PostAsync(String.Format("Training/projects/{0}/images/image?tagIds=Default", _projectId), content);
}
HttpContent data = response.Content;
string result = await data.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CreateImageResult>(result);
}
}
这个请求让我UnkonwnMediaType
,我想知道我错过了什么。
_image
参数是有效的image/jpeg
字节数组。
自己找到了答案。
public async Task<CreateImageResult> CreateImagesFromData(byte[] _image)
{
HttpContent bytesContent = new ByteArrayContent(_image);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Training-key", _trainingKey);
HttpResponseMessage response;
using (var content = new MultipartFormDataContent())
{
content.Add(bytesContent, "Image", "Image");
response = await client.PostAsync(String.Format("Training/projects/{0}/images/image?tagIds=Default", _projectId), content);
}
HttpContent data = response.Content;
string result = await data.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CreateImageResult>(result);
}
}