Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC file upload working on local build, NOT working after deployment

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 }));
}
like image 500
sri Avatar asked Aug 05 '15 12:08

sri


2 Answers

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.

like image 182
jp2code Avatar answered Oct 15 '22 01:10

jp2code


Might want to check what your application pool is running as, and if it has permissions to write files.

like image 43
elsausmc Avatar answered Oct 15 '22 01:10

elsausmc