简体   繁体   English

如何从web api中的angular post读取文件?

[英]How to read file from angular post in web api?

I successfully implemented the OWIN based token authentication. 我成功实现了基于OWIN的令牌认证。

But before implementing this, when I try to get posted files multipart form data from my angular app to web api by using HttpContext.Current.Request.Files[i].FileName , then it easily gives me posted file that I want. 但在实现之前,当我尝试通过使用HttpContext.Current.Request.Files[i].FileName将我的角度应用程序中的多部分表单数据发布到web api时,它很容易给我发布我想要的文件。

But after implementing owin based token authentication I am unable to get posted file and an error occurs on same line. 但是在实现基于owin的令牌认证后,我无法获得发布的文件,并且在同一行上发生错误。

By spending more days on this error I have found most of the answers from internet but I still can't solve my problem. 通过在这个错误上花费更多的时间,我从互联网上找到了大部分答案,但我仍然无法解决我的问题。

And most of the solutions offered say that you cannot access HttpContext.Current.Request in a self-hosted container like 并且提供的大多数解决方案都表示您无法在自托管容器中访问HttpContext.Current.Request

app.UseWebApi(config);

I am really stuck on this, so please help me. 我真的很困惑,所以请帮助我。

The error is: 错误是:

The line 306 is : HttpContext.Current.Request.Files[i].FileName 第306行是:HttpContext.Current.Request.Files [i] .FileName

An error has occurred. 发生了错误。
ExceptionMessage: This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked. ExceptionMessage:调用HttpRequest.GetBufferlessInputStream后,不支持此方法或属性。
ExceptionType: System.Web.HttpException ExceptionType:System.Web.HttpException
StackTrace 堆栈跟踪
at System.Web.HttpRequest.EnsureFiles() 在System.Web.HttpRequest.EnsureFiles()
at System.Web.HttpRequest.get_Files() 在System.Web.HttpRequest.get_Files()
at Nullplex.Rest.Controllers.UserBasicController.UpdateUserBasicByID(UserBasic UserBasic) in d:\\BitBucket\\PayBackRestApp \\Nullplex.Rest\\Controllers\\UserBasicController.cs:line 306 在D:\\ BitBucket \\ PayBackRestApp \\ Nullplex.Rest \\ Controllers \\ UserBasicController.cs中的Nullplex.Rest.Controllers.UserBasicController.UpdateUserBasicByID(UserBasic UserBasic):第306行
at lambda_method(Closure , Object , Object[] ) 在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.c__DisplayClass10 .b__9(Object instance,Object [] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor .ActionExecutor.Execute(Object instance, Object[] arguments) 在System.Web.Http.Controllers.ReflectedHttpActionDescriptor .ActionExecutor.Execute(Object instance,Object [] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor .ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken ) 在System.Web.Http.Controllers.ReflectedHttpActionDescriptor .ExecuteAsync(HttpControllerContext controllerContext,IDictionary2参数,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) 在System.Runtime .CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\\ r \\ n在System.Runtime.CompilerServices .TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
at System.Runtime.CompilerServices .TaskAwaiter1.GetResult() 在System.Runtime.CompilerServices .TaskAwaiter1.GetResult()
at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() 在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.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\\ r \\ n在System.Runtime .CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\\ r \\ n在System.Runtime .CompilerServices.TaskAwaiter1.GetResult()
at System.Web.Http.Controllers.ActionFilterResult. 在System.Web.Http.Controllers.ActionFilterResult。 d__2.MoveNext() d__2.MoveNext()

I read file posted from angular in web api like this 我在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();

now buffer contains the file data as byte[] now buffer包含文件数据byte[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM