CodeGo.net>如何从Web API的角度发布读取文件?
作者:互联网
我成功实现了基于OWIN的令牌认证.
但是在执行此操作之前,当我尝试使用HttpContext.Current.Request.Files [i] .FileName从我的角度应用程序到Web api的多部分表单数据发布文件时,它很容易为我提供了我想要的已发布文件.
但是在实现基于owin的令牌身份验证之后,我无法获取发布的文件,并且在同一行上发生错误.
通过花更多的时间解决这个错误,我从互联网上找到了大多数答案,但仍然无法解决我的问题.
而且大多数提供的解决方案都说您无法在自托管容器中访问HttpContext.Current.Request,例如
app.UseWebApi(config);
我真的很坚持,所以请帮助我.
错误是:
The line 306 is : HttpContext.Current.Request.Files[i].FileName
An error has occurred.
ExceptionMessage: This method or
property is not supported after HttpRequest.GetBufferlessInputStream
has been invoked.
ExceptionType: System.Web.HttpException
StackTrace
at System.Web.HttpRequest.EnsureFiles()
at System.Web.HttpRequest.get_Files()
at Nullplex.Rest.Controllers.UserBasicController.UpdateUserBasicByID(UserBasic
UserBasic) in d:\BitBucket\PayBackRestApp
\Nullplex.Rest\Controllers\UserBasicController.cs:line 306
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.c__DisplayClass10
.b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor
.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor
.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2
arguments, CancellationToken cancellationToken )End of stack trace from previous location where exception was thrown
at System.Runtime .CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)\r\n at System.Runtime.CompilerServices
.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices .TaskAwaiter1.GetResult()
at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()End of stack trace from previous location where exception was thrown
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task
task)\r\n at System.Runtime
.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task)\r\n at System.Runtime
.CompilerServices.TaskAwaiter1.GetResult()
at System.Web.Http.Controllers.ActionFilterResult. d__2.MoveNext()
解决方法:
我在Web API中读取了从angular发布的文件
if (!this.Request.Content.IsMimeMultipartContent("form-data"))
{
var provider = new MultipartMemoryStreamProvider();
await this.Request.Content.ReadAsMultipartAsync(provider);
var content = provider.Contents.First();
var buffer = await content.ReadAsByteArrayAsync();
现在缓冲区包含文件数据为byte []
标签:angularjs,owin,asp-net-web-api,c,net 来源: https://codeday.me/bug/20191027/1945292.html