Muhammad Yasir Bilal
Muhammad Yasir Bilal

Reputation: 13

I uploaded an asp.net MVC application on azure its not retrieving images from folder at server

I have created an ASP.NET MVC application and uploaded on azure , its images are saving inside a folder and they are working fine on my local server. but when I upload my site to azure it does not shows images that I upload online but it shows images that I had uploaded on local server. the images save code is as follows

 public JsonResult MainCategoryImageUpload(CreateMainCategoryViewModel model)
    {
        var file = model.ImageFile;
        string imgUrlWithoutTildSymbol = "";
        if (file != null)
        {
            // save mainCategory Without Image
            MainCategory m = new MainCategory();
            m.DateCreated = DateTime.Now;
            m.DateModified = DateTime.Now;
            m.MainCategoryName = model.MainCategoryName;


            ApplicationDbContext db = new ApplicationDbContext();
            db.MainCategories.Add(m);
            db.SaveChanges();

            // Now lets save image for the above category
            //var fileName = Path.GetFileName(file.FileName);
            var extension = Path.GetExtension(file.FileName);
            //var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FileName);
            //var path = Path.Combine(Server.MapPath("~/Content/Images/UploadedImage/"), fileName);
            //file.SaveAs(path);

            string id_and_extension = m.MainCategoryId + extension;
            string imgUrl = "~/Content/Images/MainCategories/" + id_and_extension;

            m.ImageUrl = imgUrl;
            db.Entry(m).State = EntityState.Modified;
            db.SaveChanges();
            file.SaveAs(Server.MapPath("~/Content/Images/MainCategories/" + id_and_extension));
            imgUrlWithoutTildSymbol = "/Content/Images/MainCategories/" + id_and_extension;

        }
         return Json(imgUrlWithoutTildSymbol,JsonRequestBehavior.AllowGet);
    }

Upvotes: 1

Views: 658

Answers (1)

Fei Han
Fei Han

Reputation: 27793

Firstly, you can use Kudu console to check if the directory /Content/Images/MainCategories/ is existing. And you can remote debug your web app and check if the code work as expected.

Secondly, I am using the following code to upload and display image, which works fine both on local and Azure, you can refer to it.

Controller

[HttpGet]
public ActionResult UploadFile()
{

    return View();
}

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
    try
    {
        if (file.ContentLength > 0)
        {
            string fileName = Path.GetFileName(file.FileName);

            var path = Server.MapPath("~/Content/Images/MainCategories/");

            if (!Directory.Exists(path)) 
            {
                Directory.CreateDirectory(path);
            }

            file.SaveAs(path + fileName);              

            ViewBag.Message = "File Uploaded Successfully!!";

            ViewBag.imgurl = "/Content/Images/MainCategories/" + fileName;
        }

        return View();
    }
    catch
    {
        ViewBag.Message = "File upload failed!!";
        return View();
    }
}

View

@{  
    ViewBag.Title = "UploadFile";  
}  

<h2>UploadFile</h2>  

@using(Html.BeginForm("UploadFile","Upload", FormMethod.Post, new { enctype="multipart/form-data"}))  
{  

    <div>  
        @Html.TextBox("file", "", new {  type= "file"}) <br />  

        <input id="Submit1" type="submit" value="submit" />
        @ViewBag.Message  

        <img src="@ViewBag.imgurl" alt="" />
    </div>  

}

Besides, Azure web app in an App Service plan has storage limit, if possible, you can store the static content in Azure Blob storage.

Upvotes: 1

Related Questions