"Server Error in '/' Application" 因为我的文件路径

"Server Error in '/' Application" becuase of my filepath

我在 VS2013 社区开发了一个 MVC4 web 应用程序。在其中,用户可以从网页上传 PDF 文件,然后将其保存到数据库中。该代码在我的 VS 环境中运行良好。但是现在,当我通过网络酒店 "online" 完成项目时,我在尝试保存 PDF 文件时收到一条错误消息。

错误信息:

Server Error in '/' Application.

Could not find a part of the path '\172.21.204.201\webvol5\sr\hv8di47d6tma347\restaurangkontrollen.se\public_html\App_Data\Uploads\test.pdf'.

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 '\172.21.204.201\webvol5\sr\hv8di47d6tma347\restaurangkontrollen.se\public_html\App_Data\Uploads\test.pdf'.

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 '\172.21.204.201\webvol5\sr\hv8di47d6tma347\restaurangkontrollen.se\public_html\App_Data\Uploads\test.pdf'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +338
   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) +1430
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +205
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) +84
   Projekt_Restaurangkollen.Controllers.AdminController.NyResturang(ViewModel VM, String command) +1882
   lambda_method(Closure , ControllerBase , Object[] ) +170
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +270
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +120
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +33
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

控制器代码:

// extract only the fielname
var fileName = Path.GetFileName(TempVM.Resturang.PDF_File.FileName);
// store the file inside ~/App_Data/uploads folder
var filePath = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);
TempVM.Resturang.PDF_File.SaveAs(filePath);

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();

TempVM.Resturang.PDF_FileName = fileName;
TempVM.Resturang.PDF_Data = bytes;
TempVM.Resturang.PDF_ContentType = filePath;

cshtml代码:

<div class="pdf-upload">
@Html.TextBoxFor(u => u.Resturang.PDF_File, new { type = "file" })
</div>

该代码在 VS 中运行良好,但现在当我尝试它时 "online" 它无法运行。有人知道为什么吗?

我已经解决问题了!

我替换了:

var filePath = Path.Combine(Server.MapPath("~/App_Data/Uploads"), fileName);

有 tihs:

var coverFolderPath = HttpContext.Server.MapPath(@"~/");
var filePath = Path.Combine(coverFolderPath, fileName);