此上传图像文件的代码在本地主机上构建时工作正常。但是在部署后,它不工作并且没有给出任何错误。
我尝试将 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/