Hi
I have Button=BtnUpload and fileupload control in my page that save image in database below is code
protected void BtnUpload_Click(object sender, EventArgs e)
{
string path = Server.MapPath(".") + "\\../image/House/services\\";
string filename = System.IO.Path.GetFileName(fup1.PostedFile.FileName);
string[] validext = { ".jpg", ".png" };
string ext = System.IO.Path.GetExtension(fup1.PostedFile.FileName);
if (Array.IndexOf(validext, ext.ToLower()) < 0)
{
Label21.Text = "please insert .jpg and .png format";
return;
}
while (System.IO.File.Exists(path + "\\" + filename))
{
filename = "1" + filename;
}
SqlCommand _cmd = new SqlCommand("fupS1", _cn);
_cmd.CommandType = CommandType.StoredProcedure;
string data = Server.UrlDecode(Request.QueryString["BehCode"]);
_cn.Open();
_cmd.Parameters.AddWithValue("@image", filename);
_cmd.Parameters.AddWithValue("@Behcode", data);
_cmd.Parameters.AddWithValue("@id", Convert.ToInt32(ViewState["Id"]));
object o = _cmd.ExecuteScalar();
int ID = (o != null && o != DBNull.Value) ? Convert.ToInt32(o) : 0;
if (ID > 0)
{
ViewState["Id"] = ID.ToString();
if (fup1.HasFile)
{
try
{
if (fup1.PostedFile.ContentLength < 102400)
{
fup1.PostedFile.SaveAs(Server.MapPath("~/image/house/services") + System.IO.Path.DirectorySeparatorChar + fup1.PostedFile.FileName);
}
else
{
Label21.Text = "upload 100KB File";
return;
}
}
catch (Exception ex)
{
Label21.Text = "Some problem occurred while uploading the file. Please try after some time.";
}
}
else
{
Label21.Text = "Please choose a file to upload.";
}
fup1.PostedFile.SaveAs(path + filename);
}
_cn.Close();
SqlCommand _cmd1 = new SqlCommand("selectservice", _cn);
_cmd1.CommandType = CommandType.StoredProcedure;
_cn.Open();
_cmd1.Parameters.AddWithValue("@Behcode", data);
SqlDataReader _dr = _cmd1.ExecuteReader();
while (_dr.Read())
{
ThumbPic3.ImageUrl = "~/image/house/services/" + _dr["Image"].ToString();
}
_cn.Close();
}
but when I upload image with Fileupload and click on btnupload button below error happen..
Server Error in '/' Application.
Access to the path 'C:\Inetpub\vhosts\behtop.com\httpdocs\image\House\services\8.jpg' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\Inetpub\vhosts\behtop.com\httpdocs\image\House\services\8.jpg' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Source Error:
Line 273:
Line 274:
Line 275: fup1.PostedFile.SaveAs(path + filename); Line 276:
Line 277: }
|
why this happen?
How I can solve this problem?
Best Regards
Neda