1
我正在嘗試編寫一個關於讀取圖像列表的代碼,並使用Cognitive Service MVC.NET從它獲取信息。獲取圖像信息Cognitve服務 - 錯誤請求ASP.NET MVC c#
我寫了這個代碼:
public async Task<ActionResult> Index()
{
List<string> list = await ReadImages();
return View(list);
}
private async Task<List<string>> ReadImages()
{
List<string> list = new List<string>();
string[] photoEntries = Directory.GetFiles(_photoFolder);
foreach (string photo in photoEntries)
{
list.Add(await GetCaptionAsync(photo));
}
return list;
}
private async Task<string> GetCaptionAsync(string photo)
{
using (var client = new HttpClient())
{
var queryString = HttpUtility.ParseQueryString(string.Empty);
//setup HttpClient
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey);
queryString["visualFeatures"] = "Categories";
queryString["details"] = "Celebrities";
queryString["language"] = "en";
var uri = "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?" + queryString;
HttpResponseMessage response;
byte[] byteData = Encoding.UTF8.GetBytes(photo);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
}
return response.Content.ToString();
}
}
的觀點是:
@model List<string>
@foreach (var item in Model)
{
@item
}
我收到了一個錯誤:在該行400錯誤請求:
response = await client.PostAsync(uri, content);
我不知道這段代碼有什麼問題,請有人幫我解釋一下這個問題? 謝謝
你要發送的文件名不是實際的文件。 – Crowcoder
如何解決這個問題? –
File.ReadAllBytes(照片)應該工作。 – Crowcoder