Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Windows.Web.Http将文件从UWP应用发送到WebAPI Web服务控制器_C#_Asp.net Web Api_Uwp_Windows 10 Universal - Fatal编程技术网

C# 无法使用Windows.Web.Http将文件从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

我的Windows 10 UWP应用程序中有以下代码,用于将文件发送到WebAPI web服务

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内核
  • 超宽带
在asp.net core中,我有一个控制器,看起来像:

[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内核
  • 超宽带
在asp.net core中,我有一个控制器,看起来像:

[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”)