上传图像在服务器上不起作用

Nei*_*eil 7 c# asp.net

在将文件从本地计算机上传到服务器时,它正在向我显示

"Server Error in '/' Application.
Access to the path 'G://images\blog-image2.jpg' is denied."
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我....请.我的c#代码是这样的:

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");
}
Run Code Online (Sandbox Code Playgroud)

这有什么不对?

小智 1

您需要确保您使用的帐户有权访问该文件路径。

此外,为 IIS 用户帐户(我相信是 IUSR)授予对该文件夹的访问权限可能会有所帮助。