Image upload does not work on server

When uploading a file from the local computer to the server, it shows me

"Server Error in '/' Application. Access to the path 'G://images\blog-image2.jpg' is denied." 

Can someone help me with this .... Please. my c # code:

 protected void btnSubmit_Click(object sender, EventArgs e) { //Get Filename from fileupload control string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName); //Save images into Images folder fileuploadimages.SaveAs(Server.MapPath("~/images/" + filename)); //Getting dbconnection from web.config connectionstring SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()); //Open the database connection con.Open(); //Query to insert images path and name into database SqlCommand cmd = new SqlCommand("Insert into tblimgs(ImageName,ImagePath) values(@ImageName,@ImagePath)", con); //Passing parameters to query cmd.Parameters.AddWithValue("@ImageName", filename); cmd.Parameters.AddWithValue("@ImagePath", "~/images/" + filename); cmd.ExecuteNonQuery(); //Close dbconnection con.Close(); Response.Redirect("default.aspx"); } 

What's bad about it?

+6
source share
2 answers

You need to make sure that the account you are using has access to this file path.

In addition, it will probably help to provide access to this folder for the IIS user account (IUSR, I believe).

+1
source

You need to check which user your application is running from, perhaps the user account will not have the right to write to any subfolders for security reasons. Give this user write permissions to the image folder and make sure that this is the only folder that gives you write permissions to minimize security risk.

You can also reorganize the code:

 fileuploadimages.SaveAs(Path.Combine(Server.MapPath("~/images"),filename))); 

since MapPath() does not add a MapPath() backslash to the displayed path, because it has no way of knowing if the path is a directory or a file (it does not check if the path really exists).

EDIT: To provide the correct permissions for this folder, you first need to find out from the application pool for the website what the identifier in which it works is (by default, this is the application pool identifier). Find the IIS user APPPOOL\DefaultAppPool . See the article on the official IIS website for more information .

If this does not work for you, make sure your asp.net {MACHINE}\ASPNET has write access to this image location. Consider granting access rights to the resource to the ASP.NET request identifier. To do this, right-click on the folder Properties > Security Tab > Edit > Add > locations > choose your local machine > click OK > ASPNET type below "Enter the object name to select" > Click Check Names Check the boxes for the desired access (Full access). If this does not work for you, then consider doing the same with Network Service .

This should now show your local account {MACHINENAME}\ASPNET , after which you will set it to write permissions to this account.

Otherwise, if the application claims to be <identity impersonate="true"/> , the identifier will be an anonymous user (usually IUSR_MACHINENAME ) or an authenticated user.

+1
source

Source: https://habr.com/ru/post/971540/


All Articles