【发布时间】: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) +1572 参数)+27
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +291.End() +49
System.Web.Mvc.Async.WrappedAsyncResultBase
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) +101.End() +49
System.Web.Mvc.Async.WrappedAsyncResultBase
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) +101.End() +49
System.Web.Mvc.Async.WrappedAsyncResultBase
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) +291.End() +49
System.Web.Mvc.Async.WrappedAsyncResultBase
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) +221.End() +49
System.Web.Mvc.Async.WrappedAsyncResultBase
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) +291.End() +49
System.Web.Mvc.Async.WrappedAsyncResultBase
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