如何在网络 api 中从 angular post 读取文件?
How to read file from angular post in web api?
我成功实现了基于 OWIN 的令牌身份验证。
但在实施此操作之前,当我尝试使用 HttpContext.Current.Request.Files[i].FileName
从我的 angular 应用程序向网络 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()
我在网络 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[]
我成功实现了基于 OWIN 的令牌身份验证。
但在实施此操作之前,当我尝试使用 HttpContext.Current.Request.Files[i].FileName
从我的 angular 应用程序向网络 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()
我在网络 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[]