Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4asp.net-mvc-3

Image is not saved in the folder on Server ASP.NET C# MVC


I am saving the picture in the folder in asp.net mvc on local server it is working fine and image is saved in the folder, but on the server image is not saved in the folder I don't know about the issue, but without picture product is uploaded successfully on the server too. here is my code for saving pic in the folder.

public ActionResult Package(PackageDO model, HttpPostedFileBase file)
    {
        try
        {
                if (file != null)
                {
                    fileName = System.Guid.NewGuid().ToString() + System.IO.Path.GetExtension(file.FileName);
                    string physicalPath = Path.Combine(Server.MapPath("~/Images/Uploads/" + fileName));
                    // save image in folder
                    file.SaveAs(physicalPath);
                }
         }
    }

My View looks like

@using (Html.BeginForm("Package", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", id = "form1" }))
{        
   <input type="file" id="file" name="file">
}

Thanks in advance.


Solution

  • Before uploading anything on server If your folder is available on server then you should have to correct read and write permission to it on server. Once permission will available then you can easily upload your files in that directory.

    You can follow Microsoft IIS guidelines for Permission here.

    I have faced this type of issue with Godaddy server. I have given the permission and it worked.

    So, Set proper permissions for the upload folder.

    Thanks