2016-03-01 20 views
0

我有一個.txt文件,其大小可達2GB,我想從客戶端上傳文件到服務器上的文件夾,同時上傳該文件時出現錯誤,是頁這樣說:在MVC中上傳大文件遇到錯誤

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文件中,我已經設置maxRequestLengthexecutionTimeout,並且maxAllowedContentLength配置。 也許我的代碼是錯誤的,你們可以給我更好的方式來上傳這樣的文件嗎? 謝謝, 對不起,我的英語不好。

+0

看看這個主題http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net –

+0

@Mpollack這是我的設置:,是不是有什麼問題? – TeachMe

+0

你能再次檢查路徑嗎?你在哪裏存儲你的文件?那路徑實際存在? –

回答

0
<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

+0

感謝您的回覆,但它仍然不起作用 – TeachMe

+0

檢查了這一點 http://www.iis.net/configreference/system .webserver/asp/limits 它可能是網絡服務器 –

0

您絕對不應該允許上傳大型文件到Web服務器,因爲它會佔用所有內存並導致您的Web服務器崩潰。請記住,當您將文件上傳到Web服務器時,所有數據都會在嘗試寫入數據庫或文件系統時存儲在內存中。對於文件較大的文件,您需要通過文件流或通過FTP上傳文件。如果你是谷歌喬恩加洛韋,他可能會有其他方式上傳大文件的一些答案。

+0

感謝您的回覆,也許我會用戶NeatUpload或其他方式。 – TeachMe