c# - ASP.NET MVC 文件上传在本地构建上工作,在部署后不工作

标签 c# asp.net asp.net-mvc asp.net-mvc-5

此上传图像文件的代码在本地主机上构建时工作正常。但是在部署后,它不工作并且没有给出任何错误。

我尝试将 imagePath 从 /Content/Images/Items/ 更改为 ~/Content/Images/Items/Content/Images/Items/。仍然没有解决方案。

[HttpPost]
public ActionResult AddProduct(ProductDisplay productDisplay,HttpPostedFileBase upload)
{
    bool isSaved = false;
    string fileName = string.Empty;
    string imagePath = string.Empty;
    try
    {
        if(upload!=null && upload.ContentLength>0)
        {
            fileName = System.IO.Path.GetFileName(upload.FileName);
            imagePath = "/Content/Images/Items/" + fileName;
            upload.SaveAs(Server.MapPath(imagePath));
        }
        else
            imagePath = "/Content/Images/Items/" + "NoImage.jpg";
        productDisplay.ImagePath = imagePath;
        ProductMangementBL balProduct = new ProductMangementBL();
        isSaved = balProduct.AddProduct(productDisplay);
    }
    catch (Exception ex)
    {
        isSaved = false;
    }
    return RedirectToAction("ProductList", new RouteValueDictionary(new { controller = "Product", action = "ProductList", Id = productDisplay.CategoryID }));
}

最佳答案

一些要检查的东西:

var mapped = Server.MapPath(imagePath);
if (File.Exists(mapped))
{
  //
}
else
{
  throw new FileNotFoundException(imagePath);
}

换句话说,图像可能不存在。

关于c# - ASP.NET MVC 文件上传在本地构建上工作,在部署后不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31833138/

相关文章:

c# - 如何只分析一个类库?

c# - C# 中的复合赋值 ^= 是原子的吗?

c# - 从托管 C++ 到 C# 的结构列表

asp.net - 我应该为 ASP.net 创建一个 JQuery 服务器控件以便在我的应用程序中最好地使用它吗?

c# - 如何在 AutoMapper 中使用 foreach 循环

asp.net - 如何在asp.net和C#中实现登录 session

asp.net - 在 ASP.NET C# 中自动引发事件以发送电子邮件

asp.net-mvc - ASP.NET MVC3 IIS7.5 : Cache-Control maxage is always 0 (not good for client-side caching)

asp.net-mvc - 如何在同一 Controller ( Entity Framework )中使用两个模型并在不同的操作中使用每个模型?

asp.net - 如何像ajax一样向 View 发送消息