【问题标题】:Upload Large File In MVC Encountered an Error在 MVC 中上传大文件遇到错误
【发布时间】:2016-03-01 04:06:52
【问题描述】:

我有一个最大为 2GB 的 .txt 文件,我想将该文件从客户端上传到服务器中的文件夹,在上传该文件时出现错误,这是页面上的内容:

An operation was attempted on a nonexistent network connection. (Exception from HRESULT: 0x800704CD) 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.Runtime.InteropServices.COMException: An operation was attempted on a nonexistent network connection. (Exception from HRESULT: 0x800704CD)

Source Error: 


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 



[COMException (0x800704cd): An operation was attempted on a nonexistent network connection. (Exception from HRESULT: 0x800704CD)]

[HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x800704CD.]
   System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) +3399867
   System.Web.Hosting.IIS7WorkerRequest.ReadEntityCoreSync(Byte[] buffer, Int32 offset, Int32 size) +9621484
   System.Web.Hosting.IIS7WorkerRequest.ReadEntityBody(Byte[] buffer, Int32 size) +24
   System.Web.HttpRequest.GetEntireRawContent() +315
   System.Web.HttpRequest.GetMultipartContent() +63
   System.Web.HttpRequest.FillInFormCollection() +160
   System.Web.HttpRequest.EnsureForm() +69
   System.Web.HttpRequest.get_Form() +13
   System.Web.HttpRequestWrapper.get_Form() +14
   System.Web.Mvc.HttpRequestExtensions.GetHttpMethodOverride(HttpRequestBase request) +121
   System.Web.Mvc.AcceptVerbsAttribute.IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) +37
   System.Web.Mvc.HttpGetAttribute.IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) +39
   System.Web.Mvc.ActionMethodSelectorBase.IsValidMethodSelector(ReadOnlyCollection`1 attributes, ControllerContext controllerContext, MethodInfo method) +54
   System.Web.Mvc.ActionMethodSelectorBase.RunSelectionFilters(ControllerContext controllerContext, List`1 methodInfos) +132
   System.Web.Mvc.ActionMethodSelectorBase.FindActionMethods(ControllerContext controllerContext, String actionName) +142
   System.Web.Mvc.ActionMethodSelectorBase.FindActionMethod(ControllerContext controllerContext, String actionName) +104
   System.Web.Mvc.Async.ReflectedAsyncControllerDescriptor.FindAction(ControllerContext controllerContext, String actionName) +54
   System.Web.Mvc.ControllerActionInvoker.FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, String actionName) +16
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +114
   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) +468
   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__3(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.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是一些代码:

    public ActionResult UploadFile_Post(FormCollection dt, string btn, HttpPostedFileBase uploadFile, FileUpload dataFile)
    {
      string nameFile, getExtension = "";
      nameFile = System.IO.Path.GetFileNameWithoutExtension(uploadFile.FileName.ToString().Trim()) + "_" + usernm.Trim() + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss").Trim();
      getExtension = System.IO.Path.GetExtension(uploadFile.FileName.ToString().Trim());

      string FilePath = Server.MapPath("~\\Upload\\" + nameFile + ".txt");
      string pathFile = Server.MapPath("~\\Upload\\");

      if (System.IO.File.Exists(FilePath))
      {
           System.IO.File.Delete(FilePath);                            
      }
      uploadFile.SaveAs(FilePath);//i think failed at this process
   }

在 Web.config 文件中,我已经设置了 maxRequestLengthexecutionTimeoutmaxAllowedContentLength 的配置。 也许我的代码是错误的,你们能给我更好的方法来上传这样的文件吗? 谢谢你, 对不起我的英语不好。

【问题讨论】:

  • @Mpollack 这是我的设置: 和这个 ,有什么问题吗?
  • 你能再检查一下路径吗?你在哪里存储你的文件?这条路真的存在吗?
  • @Justcode 是的,它存在,文件夹名称是“上传”,在发布文件夹内。该错误发生在第 11 分钟。
  • 小文件也会出现这个错误吗?

标签: c# asp.net-mvc file-upload flat-file


【解决方案1】:
<system.web>
    <httpRuntime executionTimeout="3600" maxRequestLength="102400" 
     appRequestQueueLimit="100" requestLengthDiskThreshold="10024000"/>
</system.web>

executionTimeout : 指定请求在被 ASP.NET 自动关闭之前允许执行的最大秒数。

maxRequestLength : 指定输入流缓冲阈值的限制,单位为 KB

appRequestQueueLimit :指定 ASP.NET 为应用程序排队的最大请求数。

requestLengthDiskThreshold:指定输入流缓冲阈值的限制,以字节为单位。此值不应超过 maxRequestLength 属性。

更改这些设置,看看会发生什么。知道您正在使用的 IIS 版本也很好。 https://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx

【讨论】:

【解决方案2】:

您永远不应该允许将这么大的文件上传到网络服务器,因为它会占用所有内存并使您的网络服务器崩溃。请记住,当您将文件上传到 Web 服务器时,所有数据都会在尝试写入数据库或文件系统时存储在内存中。对于较大的文件,您需要通过文件流或通过 FTP 上传文件。如果你用谷歌搜索 Jon Galloway,他可能会为你提供一些关于上传大文件的其他方法的答案。

【讨论】:

  • 感谢您的回复,也许我会使用 NeatUpload 或其他方式。
猜你喜欢
  • 1970-01-01
  • 2010-10-16
  • 2023-02-23
  • 1970-01-01
  • 2022-01-26
  • 2015-11-09
  • 2014-08-06
  • 2015-02-20
  • 2012-03-22
相关资源
最近更新 更多