2017-02-14 15 views
0

我在嘗試將圖像上傳到我的網站時出現以下錯誤。服務器錯誤 - 無法找到部分路徑'C: home site wwwroot App_Data Temp 1.png'

Server Error in '/' Application.

Could not find a part of the path 'C:\home\site\wwwroot\App_Data\Temp\1.png'.

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.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\home\site\wwwroot\App_Data\Temp\1.png'.

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:

[DirectoryNotFoundException: Could not find a part of the path 'C:\home\site\wwwroot\App_Data\Temp\1.png'.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +353 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1326 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +60
System.IO.FileStream..ctor(String path, FileMode mode) +55
System.Web.HttpPostedFile.SaveAs(String filename) +94
System.Web.HttpPostedFileWrapper.SaveAs(String filename) +14
RoomAuditSystem.Controllers.HomeController.Index(HttpPostedFileBase file) +96 lambda_method(Closure , ControllerBase , Object[]) +104 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 parameters) +157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +27
System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +50 System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +225 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9744373 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

這裏是代碼處理該部分:

[HttpPost] 
    public ActionResult Index(HttpPostedFileBase file) 
    { 
     if (file.ContentLength > 0) 
     { 
      var fileName = Path.GetFileName(file.FileName); 
      var path = Path.Combine(Server.MapPath("~/App_Data/Temp"), fileName); 
      file.SaveAs(path); 

      // create a barcode reader instance 
      IBarcodeReader reader = new BarcodeReader(); 
      // load a bitmap 
      Bitmap bitmap = new Bitmap(path); 

      Bitmap resized = new Bitmap(bitmap, new Size(400, 400)); 

      // detect and decode the barcode inside the bitmap 
      var result = reader.Decode(resized); 

      bitmap.Dispose(); 

      // Delete file once we have finished with it 
      if (System.IO.File.Exists(path)) 
      { 
       System.IO.File.Delete(path); 
      } 

      // do something with the result 
      if (result != null) 
      { 
       return RedirectToAction("Create", "Audit", new { roomID = result.Text }); 
       //txtDecoderType.Text = result.BarcodeFormat.ToString(); 
      } 

      // Decode error 
      else 
      { 
       return RedirectToAction("Index"); 
      } 
     } 

     // File error 
     else 
     { 
      return RedirectToAction("Index"); 
     }  
    } 

它工作正常,我的本地機器上,但似乎並沒有要當我在網上發佈工作。我肯定有一個名爲App_Data的目錄,在我的網站的根目錄中有一個名爲Temp的目錄。

任何幫助,非常感謝。

+0

你在App_Data中有Temp文件夾嗎? –

+0

什麼是您的App_Data \ Temp目錄中的圖像設置爲Build Action? –

+0

您是否檢查過目標目錄上的權限? –

回答

3

例外是明確的。部分路徑不存在。重要的是,發佈期間不包含App_Data,所以僅僅因爲它在本地存在於您的項目中,並不意味着它實際上存在於您的生產服務器上。除此之外,檢查整個路徑,即你必須直接有home目錄C驅動器,它必須有一個site目錄等對

此外,這裏還有沒有辦法讓異常,如果一切是正確的。該路徑的某些部分不正確或不存在。

+0

感謝您的輸入。我現在刪除了App_Data,並將Temp移動到我的應用程序的根目錄並再次發佈。我相信C:\ home \ site \ wwwroot位是由我託管的公司製作的,但它仍然無法工作。 – James

0

管理解決這個與克里斯的解釋。即使我將它與整個應用程序一起發佈,「Temp」文件夾也不存在。我假設,因爲這個文件夾是空的,它沒有與其他一切發表。所以我手動發佈了這個單獨的文件夾,並嘿presto - 它的作品!

相關問題