【问题标题】:Server Error - Could not find a part of the path 'C:\home\site\wwwroot\App_Data\Temp\1.png'服务器错误 - 找不到路径“C:\home\site\wwwroot\App_Data\Temp\1.png”的一部分
【发布时间】:2017-02-14 19:27:52
【问题描述】:

我在尝试将图片上传到我的网站时遇到以下错误。

“/”应用程序中的服务器错误。

找不到路径的一部分 'C:\home\site\wwwroot\App_Data\Temp\1.png'。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.IO.DirectoryNotFoundException:无法 找到路径“C:\home\site\wwwroot\App_Data\Temp\1.png”的一部分。

来源错误:

在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。

堆栈跟踪:

[DirectoryNotFoundException: 找不到路径的一部分 '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 选项,SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1326 System.IO.FileStream..ctor(字符串路径,文件模式 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize、 FileOptions 选项,字符串 msgPath,布尔 bFromProxy) +60
System.IO.FileStream..ctor(字符串路径,文件模式模式)+55
System.Web.HttpPostedFile.SaveAs(字符串文件名) +94
System.Web.HttpPostedFileWrapper.SaveAs(字符串文件名) +14
RoomAuditSystem.Controllers.HomeController.Index(HttpPostedFileBase 文件)+96 lambda_method(闭包,ControllerBase,对象[])+104 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext 控制器上下文,IDictionary2 parameters) +157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 参数)+27
System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult2.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.WrappedAsyncResult1.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.WrappedAsyncResult1.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 内部状态)+13
System.Web.Mvc.Async.WrappedAsyncVoid1.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,控制器控制器)+12
System.Web.Mvc.Async.WrappedAsyncVoid1.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.WrappedAsyncVoid1.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 结果)+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 的目录,位于我网站的根目录中。

非常感谢任何帮助。

【问题讨论】:

  • App_Data 中有 Temp 文件夹吗?
  • App_Data\Temp 目录中的图像的 Build Action 设置是什么?
  • 您是否检查了目标目录的权限?
  • @CamBruce 这是什么意思?你能详细说明一下吗?是的,Ehsan,我确实有那个文件夹。
  • 每个文件在 Visual Studio 的“属性”窗口中都有一个“构建操作”属性。它通常会根据文件的类型设置为“构建”或“内容”

标签: c# asp.net asp.net-mvc file asp.net-mvc-4


【解决方案1】:

异常是明确的。该路径的一部分不存在。重要的是,App_Data 不包含在发布过程中,因此仅仅因为它存在于您本地的项目中,并不意味着它实际上存在于您的生产服务器上。除此之外,请检查整个路径,即您的 C 盘上必须直接有一个 home 目录,该目录必须有一个 site 目录等。

同样,如果一切正常,就没有办法获得该异常。该路径的某些部分不正确或不存在。

【讨论】:

  • 感谢您的意见。我现在已删除 App_Data 并将 Temp 移动到我的应用程序的根目录并再次发布。我相信 C:\home\site\wwwroot 位是由我托管的公司制作的,但它仍然无法正常工作。
【解决方案2】:

设法通过克里斯的解释解决了这个问题。即使我将它与整个应用程序一起发布,文件夹“Temp”也不存在。我假设因为这个文件夹是空的,所以它没有与其他所有内容一起发布。所以我手动发布了那个单独的文件夹,嘿,它的工作原理!

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2021-12-22
    • 2018-01-17
    • 1970-01-01
    • 2011-03-05
    • 2017-01-10
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多