Cannot get file sent from UWP app using Windows.Web.Http to WebAPI web service controller

余生长醉 提交于 2019-12-20 06:47:50

问题


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();
}

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)
    {
        //...
    }
}

I also tried using IFormFile with the same result.

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

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


回答1:


As promised! what i used:

  • asp.net core
  • uwp

in the asp.net 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 https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads
            return Task.FromResult<IActionResult>(Ok());
        }
    }

in UWP use this to send the image:

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("http://localhost:15924/api/values/Form"), formContent);
           response.EnsureSuccessStatusCode();
        }
        catch(Exception ex)
        {

        }

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



来源:https://stackoverflow.com/questions/49577563/cannot-get-file-sent-from-uwp-app-using-windows-web-http-to-webapi-web-service-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!