0
votes

I am trying to upload an image.it works fine when i do it from my localhost but when i published it it throw an error from server:

When I use this code :

public string ImagePath(HttpPostedFileBase imgfile)
        {
            var path = "";
            // code for saving the image file to a physical location.
            var fileName = Path.GetFileName(imgfile.FileName);


            path = Path.Combine(HttpContext.Server.MapPath("~/Images/Sections/Developer/ClientLogo"), fileName);

            string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imgfile.FileName);
            int iteration = 1;
            while (System.IO.File.Exists((path)))
            {
                fileName = string.Concat(fileNameWithoutExtension, "-", iteration, System.IO.Path.GetExtension(imgfile.FileName));

                path = Path.Combine(HttpContext.Server.MapPath("~/Images/Sections/Developer/ClientLogo"), fileName);
                iteration++;
            }
            imgfile.SaveAs(path);
            // prepare a relative path to be stored in the database and used to display later on.
            path = Url.Content(Path.Combine("~/Images/Sections/Developer/ClientLogo", fileName));
            return path;
        }

Error is

System.UnauthorizedAccessException: Access to the path 'D:\InetPub\vhosts\xx.com\httpdocs\Images\Sections\Developer\ClientLogo\circle-small-empty.18x18.png' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at System.Web.HttpPostedFileWrapper.SaveAs(String filename) at xx.CorporateSite.Controllers.DeveloperController.ImagePath(HttpPostedFileBase imgfile)

And when I use Server.MapPath instead of HttpContext.Server.MapPath it throw different error:

Error is:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\InetPub\vhosts\xx.com\httpdocs\Images\Sections\Developer\ClientLogo\demo.png'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at System.Web.HttpPostedFileWrapper.SaveAs(String filename) at xx.CorporateSite.Controllers.DeveloperController.ImagePath(HttpPostedFileBase imgfile)

I tried to change the permission from my localhost but nothing is working...Please suggest me something

2

2 Answers

2
votes

Your web application does not have permission to write to the location where you're trying to save the image. This is often handled by adding an entry to your web.config which points to a folder where all uploads are saved

<appSettings>
  ...
  <add key="uploadPath" value="C:\Uploads"/>
  ...
</appSettings>

and then in your code you would read that configuration entry to identify the path where the image will be saved:

....
string path = ConfigurationManager.AppSettings["uploadPath"];
string filePath = Path.Combine(path, fileName);
....

And then in order to save the files to this directory, you would need to set permissions on the directory so that the user that the web application is running as has Write permissions on that directory. This will allow you to write files from the web application to that folder.

This way, you, as the developer, are not dictating where the files go. The system administrator can decide both where the files go, and what permissions are necessary to support your web application.

0
votes

Check to see if the folder's Read-Only attribute is active. If it is uncheck it in the properties of the folder if this is a windows machine.

If it keeps reverting you may need to take ownership of the folder by going to security->advanced->effective permissions->click select...->put in your username and check full control->then click ok out.