这个上传图像文件的代码在构建于localhost上时运行良好。但在部署时,它不工作,也不会给出任何错误。
我尝试将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 }));
}发布于 2015-08-05 20:59:55
以下是需要检查的内容:
var mapped = Server.MapPath(imagePath);
if (File.Exists(mapped))
{
//
}
else
{
throw new FileNotFoundException(imagePath);
}换句话说,图像可能不在那里。
发布于 2015-08-05 20:59:04
可能需要检查应用程序池是以什么身份运行的,以及它是否具有写入文件的权限。
发布于 2015-08-05 21:12:42
您需要创建testProductAdd操作和相应的测试视图。
然后试着只运行这段代码而不进行任何异常处理。
fileName = System.IO.Path.GetFileName(upload.FileName);
imagePath = "/Content/Images/Items/" + fileName;
upload.SaveAs(Server.MapPath(imagePath));这样您就可以找出问题所在。
https://stackoverflow.com/questions/31833138
复制相似问题