在本地主机上构建时,用于上传图像文件的此代码可以正常工作。但是,在部署后,它无法正常工作,并且不会出现任何错误。

我尝试将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);
}

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

10-05 21:50