C# 无法使用Windows.Web.Http将文件从UWP应用发送到WebAPI Web服务控制器
我的Windows 10 UWP应用程序中有以下代码,用于将文件发送到WebAPI web服务C# 无法使用Windows.Web.Http将文件从UWP应用发送到WebAPI Web服务控制器,c#,asp.net-web-api,uwp,windows-10-universal,C#,Asp.net Web Api,Uwp,Windows 10 Universal,我的Windows 10 UWP应用程序中有以下代码,用于将文件发送到WebAPI web服务 public async void Upload_FileAsync(string WebServiceURL, string FilePathToUpload) { //prepare HttpStreamContent IStorageFile storageFile = await StorageFile.GetFileFromPathAsync(FilePathToUploa
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]
,结果相同
public class MyController : Controller
{
[HttpPost]
public async Task<bool> Upload_File(byte[] myFileBytes)
{
//...
}
}
public class MyController : Controller
{
[HttpPost]
public async Task<bool> Upload_File(IFormFile myFileBytes)
{
//...
}
}
如何将文件发送到web服务中的控制器?请帮忙 如承诺的那样!我用的是:
- asp.net内核
- 超宽带
[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());
}
}
添加(fileContent,“files”,“lockscreenlogo.png”)代码>很重要。按照承诺使用此特定覆盖!我用的是:
- asp.net内核
- 超宽带
[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());
}
}
添加(fileContent,“files”,“lockscreenlogo.png”)代码>很重要。使用此特定覆盖谢谢!它使用一个文件。但是当我以同样的方式向
formContent
添加另一个文件时,它就不起作用了。控制器只在文件
列表中显示一项:/n知道它可能是什么吗?此外,如果我尝试将序列化对象作为HttpStringContent
添加到formContent
,控制器也不会显示该项。我试图使用HttpStringContent httpStringContent1=newhttpstringcontent(JsonConvert.SerializeObject(myObjectBasedOnMyClass,Formatting.Indented)、Windows.Storage.Streams.unicodeincoding.Utf8,“application/json”)然后formContent.Add(httpStringContent2,“object_1”,“object_1.txt”)代码>我以这种方式添加了另一个文件,并在控制器中接收到它:var fileContent2=new-HttpStreamContent(wait file.OpenReadAsync());fileContent2.Headers.ContentType=new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue(“image/png”);添加(fileContent2,“files”,“lockscreenlogo2.png”)代码>谢谢!它使用一个文件。但是当我以同样的方式向formContent
添加另一个文件时,它就不起作用了。控制器只在文件
列表中显示一项:/n知道它可能是什么吗?此外,如果我尝试将序列化对象作为HttpStringContent
添加到formContent
,控制器也不会显示该项。我试图使用HttpStringContent httpStringContent1=newhttpstringcontent(JsonConvert.SerializeObject(myObjectBasedOnMyClass,Formatting.Indented)、Windows.Storage.Streams.unicodeincoding.Utf8,“application/json”)然后formContent.Add(httpStringContent2,“object_1”,“object_1.txt”)代码>我以这种方式添加了另一个文件,并在控制器中接收到它:var fileContent2=new-HttpStreamContent(wait file.OpenReadAsync());fileContent2.Headers.ContentType=new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue(“image/png”);添加(fileContent2,“files”,“lockscreenlogo2.png”)代码>