实际上,我将图像作为内存流从MVC控制器发送到Web API作为内存流,而在Web API中下载映像时,我只是获得了一半映像,而不是完整图像。请在此链接中找到样品输出图像
我的MVC控制器:
public ActionResult Index(FormCollection formCollection)
{
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item] as HttpPostedFileBase;
if (file.ContentLength == 0)
continue;
if (file.ContentLength > 0)
{
MemoryStream ms = new MemoryStream();
file.InputStream.CopyTo(ms);
ms.Position = 0;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:35221");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/octent-stream"));
HttpContent content = new StreamContent(ms);
var response = client.PostAsync("Api/Ocr/GetText",content).Result;
if (response.IsSuccessStatusCode)
{
}}}}
我的Web API控制器:
[HttpPost]
public List<string> GetText(HttpRequestMessage request)
{
try
{
HttpContent content = Request.Content;
MemoryStream ms = new MemoryStream();
content.CopyToAsync(ms);
Stream stream = ms;
Bitmap bmp = new Bitmap(stream);
bmp.Save(@"C:UsersbPopuriDesktopTestTemp.jpg");
byte[] value = memorystream.ToArray();
}
对不起,我无法在此处发布我的总代码。我只是将图像从MVC发送到Web API,但是Web API中的图像未正确加载。
请帮助我如何避免此问题。
事先感谢您的帮助
您需要await
呼叫CopyToAsync
。为此,进行以下更改:
public async Task<List<string>> GetText(HttpRequestMessage request)
{
...
await content.CopyToAsync(ms);
...
}
看起来问题是由于复制操作在尝试保存文件之前未完成。