c# - 调用 HttpRequest.GetBufferlessInputStream 后不支持此方法

标签 c# asp.net file-upload iis-7

所以在 IHttpModule 中我试图获取一个无缓冲的输入流:

HttpContext.Current.Request.GetBufferlessInputStream(true);

但是我在尝试发送到 Controller 方法的文件上传时收到此错误:

调用 HttpRequest.GetBufferlessInputStream 后不支持此方法或属性。

我已经研究过类似的文章:

https://blogs.msdn.microsoft.com/praburaj/2012/09/13/httpcontext-current-request-inputstream-property-throws-exception-this-method-or-property-is-not-supported-after-httprequest-getbufferlessinputstream-has-been-invoked-or-httpcontext-cur/

How should I use HttpRequest.GetBufferlessInputStream?

但是添加 key 应用程序设置似乎没有什么不同。

这是我的堆栈跟踪:

[HttpException (0x80004005): This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked.]
   System.Web.HttpRequest.EnsureFiles() +3678728
   System.Web.HttpRequest.get_Files() +13
   System.Web.HttpRequestWrapper.get_Files() +28
   System.Web.Mvc.HttpFileCollectionValueProvider.GetHttpPostedFileDictionary(ControllerContext controllerContext) +36
   System.Web.Mvc.HttpFileCollectionValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +30
   System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +69
   System.Web.Mvc.ControllerBase.get_ValueProvider() +30
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +62
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
   System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

最佳答案

尝试使用:

var a = Request.Content.ReadAsByteArrayAsync();

a.Result 返回字节数组,然后您可以使用它来转换为您需要的内容。

关于c# - 调用 HttpRequest.GetBufferlessInputStream 后不支持此方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48776028/

相关文章:

java - 在 servlet 中上传文件

c# - 我如何通知 wpf 拥有的窗口关闭?

c# - 继承和接口(interface) .NET

c# - 复杂密码的正则表达式

javascript - 如何设置 jQuery-File-Upload?如何实现上传处理程序?

ios - 从库中上传视频 Afnetworking multipart

c# - ORA-22288 : file or LOB operation FILEOPEN failed The system cannot find the path specified

c# - HTTP 请求未经客户端身份验证方案 'Anonymous' 授权。从服务器收到的身份验证 header 是“基本”

c# - 如何合并多个pdf文件(在运行时生成)?

asp.net - Web-Api OData Controller 返回单个记录