无法使用Windows.Web.Http将文件从UWP应用发送到WebAPI Web服务控制器

编程入门 行业动态 更新时间:2024-10-28 10:33:46
本文介绍了无法使用Windows.Web.Http将文件从UWP应用发送到WebAPI Web服务控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Windows 10 UWP应用中具有以下代码,用于将文件发送到WebAPI Web服务.

I have the following code on my Windows 10 UWP app to send a file to a WebAPI web service.

public async void Upload_FileAsync(string WebServiceURL, string FilePathToUpload) { //prepare HttpStreamContent IStorageFile storageFile = await StorageFile.GetFileFromPathAsync(FilePathToUpload); IRandomAccessStream stream=await storageFile.OpenAsync(FileAccessMode.Read); Windows.Web.Http.HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(stream); //send request var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter(); myFilter.AllowUI = false; var client = new Windows.Web.Http.HttpClient(myFilter); Windows.Web.Http.HttpResponseMessage result = await client.PostAsync(new Uri(WebServiceURL), streamContent); string stringReadResult = await result.Content.ReadAsStringAsync(); }

这是我要在Web服务中尝试使用的控制器,但 myFileBytes 始终具有空值.我尝试添加具有相同结果的 [FromBody] 和 [FromForm] .

Here is the controller that I am trying to use in the web service but myFileBytes always has a null value. I tried adding [FromBody] and [FromForm] with the same result.

public class MyController : Controller { [HttpPost] public async Task<bool> Upload_File(byte[] myFileBytes) { //... } }

我还尝试使用 IFormFile 获得相同的结果.

I also tried using IFormFile with the same result.

public class MyController : Controller { [HttpPost] public async Task<bool> Upload_File(IFormFile myFileBytes) { //... } }

如何将文件获取到Web服务中的控制器?请帮忙!

How can I get the file to the controller in the web service? Please help!

推荐答案

承诺!我用的是什么:

  • asp核心
  • uwp

在asp核心中,我有一个看起来像这样的控制器:

in the asp core i have a controller that looks like:

[Route("api/[controller]")] public class ValuesController : Controller { [HttpPost("Bytes")] public void Bytes([FromBody]byte[] value) { } [HttpPost("Form")] public Task<IActionResult> Form([FromForm]List<IFormFile> files) { // see docs.microsoft/en-us/aspnet/core/mvc/models/file-uploads return Task.FromResult<IActionResult>(Ok()); } }

在UWP中使用它来发送图像:

in UWP use this to send the image:

var file =等待StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/LockScreenLogo.png"));

var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/LockScreenLogo.png"));

try { var http = new HttpClient(); var formContent = new HttpMultipartFormDataContent(); var fileContent = new HttpStreamContent(await file.OpenReadAsync()); fileContent.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("image/png"); formContent.Add(fileContent, "files", "lockscreenlogo.png"); var response = await http.PostAsync(new Uri("localhost:15924/api/values/Form"), formContent); response.EnsureSuccessStatusCode(); } catch(Exception ex) { }

formContent.Add(fileContent,"files","lockscreenlogo.png"); 很重要.使用此特定的替代

the formContent.Add(fileContent, "files", "lockscreenlogo.png"); is important. Use this specific override

更多推荐

无法使用Windows.Web.Http将文件从UWP应用发送到WebAPI Web服务控制器

本文发布于:2023-10-07 01:36:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1468071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   控制器   文件   Web   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!