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 FilePa
As promised! what i used:
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