无法使用Windows.Web.Http从UWP应用程序发送文件到WebAPIWeb服务控制器。UWP应用程序上有以下代码,用于将文件发送到WebAPIWeb服务。publicasyncvoidUpload_FileAsync(stringWebServiceURL,stringFilePathToUpload){//准备HttpStreamContentIStorageFilestorageFile=awaitStorageFile.GetFileFromPathAsync(FilePathToUpload);IRandomAccessStreamstream=awaitstorageFile.OpenAsync(FileAccessMode.Read);Windows.Web.Http.HttpStreamContentstreamContent=newWindows.Web.Http.HttpStreamContent(stream);//发送请求varmyFilter=newWindows.Web.Http.Filters.HttpBaseProtocolFilter();myFilter.AllowUI=false;varclient=newWindows.Web.Http.HttpClient(myFilter);Windows.Web.Http.HttpResponseMessageresult=awaitclient.PostAsync(newUri(WebServiceURL),streamContent);字符串stringReadResult=awaitresult.Content.ReadAsStringAsync();这是我试图在网络服务中使用的控制器,但myFileBytes始终具有空值。我尝试添加[FromBody]和[FromForm]结果相同。publicclassMyController:Controller{[HttpPost]publicasyncTaskUpload_File(byte[]myFileBytes){//...}}我也尝试使用IFormFile,结果相同。publicclassMyController:Controller{[HttpPost]publicasyncTaskUpload_File(IFormFilemyFileBytes){//...}}如何将文件上传到Web服务中的控制器?请帮忙!按照承诺!我用过的:在asp.net核心中,我有一个如下所示的控制器:[Route("api/[controller]")]publicclassValuesController:Controller{[HttpPost("Bytes")]publicvoidBytes([FromBody]byte[]value){}[HttpPost("Form")]publicTaskForm([FromForm]Listfiles){//参见https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads返回Task.FromResult(Ok());在UWP中使用它来发送图像:varfile=awaitStorageFile.GetFileFromApplicationUriAsync(newUri("ms-appx:///Assets/LockScreenLogo.png"));尝试{varhttp=newHttpClient();varformContent=newHttpMultipartFormDataContent();varfileContent=newHttpStreamContent(awaitfile.OpenReadAsync());fileContent.Headers.ContentType=新的Windows.Web。Http.Headers.HttpMediaTypeHeaderValue("图片/png");formContent.Add(fileContent,"files","lockscreenlogo.png");变量响应onse=awaithttp.PostAsync(newUri("http://localhost:15924/api/values/Form"),formContent);响应.EnsureSuccessStatusCode();}catch(Exceptionex){}formContent.Add(fileContent,"files","lockscreenlogo.png");Itisimportanttousethisspecificoverride上面是C#LearningTutorial:UnabletouseWindows.Web.HttptosendafilefromaUWPapptoaWebAPIWebServiceControllershare的全部内容,如果对大家有用的话需要了解更多C#学习教程。希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
