This code for uploading image file is working fine when built on localhost. But upon deployment, it is not working and not giving any error.
I tried changing the imagePath from /Content/Images/Items/
to ~/Content/Images/Items/
and Content/Images/Items/
. Still no solution.
[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 }));
}
Some things to check:
var mapped = Server.MapPath(imagePath);
if (File.Exists(mapped))
{
//
}
else
{
throw new FileNotFoundException(imagePath);
}
In other words, the image may not be there.
Might want to check what your application pool is running as, and if it has permissions to write files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With