如何将图像从UWP发布到.NET Core Web API?

现在,我已经配置了使用HttpClient的UWP照片发布到Web api部分。


Uri uri = new Uri("http://localhost:50040/api/Upload");

IInputStream inputStream = await photoFile.OpenAsync(FileAccessMode.Read);

HttpMultipartFormDataContent multipartContent = new HttpMultipartFormDataContent();

multipartContent.Add(new HttpStreamContent(inputStream), "myFile", photoFile.Name);

Windows.Web.Http.HttpClient newclient = new Windows.Web.Http.HttpClient();

Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(uri, multipartContent);

但是我不知道如何为服务器端设置.NET核心Web API,以获取从UWP应用程序发布的图像。请帮助我,谢谢。


慕运维8079593
浏览 165回答 2
2回答

呼如林

但是我不知道如何为服务器端设置.NET核心Web API请参考文件上传官方教程&nbsp;以创建服务器端。例如,添加POST方法,如下面的示例代码所示,以使用上面显示的客户端代码接收UWP客户端发送的文件。// POST api/values[HttpPost]public async Task<IActionResult> Post(IFormFile myFile){&nbsp; &nbsp;// full path to file in temp location, you could change this&nbsp; &nbsp;var filePath = Path.GetTempFileName();&nbsp; &nbsp;if (myFile.Length > 0)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;using (var stream = new FileStream(filePath, FileMode.Create))&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;await myFile.CopyToAsync(stream);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;// process uploaded files&nbsp; &nbsp;// Don't rely on or trust the FileName property without validation.&nbsp; &nbsp;return Ok(new { filePath, myFile.Length });}更多详细信息,您还可以参考官方样本。
打开App,查看更多内容
随时随地看视频慕课网APP