我試圖通過使用c#將圖像添加到目錄中。我將使用這個圖像在gridview中顯示。我編寫了代碼,但出現錯誤,無法解決。我有一個名爲images的directoy。這是我得到錯誤FileUpload1.SaveAs(Server.MapPath(「images /」+ FileName)。它說C:\ Users \ user \ Documents \ Visual Studio 2008 \ WebSites \ WebSite1 \ images \ im1.jpg'部分。路徑找不到的下面是我的代碼:將圖像保存到目錄c#
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("images/" + FileName));
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "INSERT INTO Books (Book_Name,Author_Name,FileName, FilePath, In_Lib) VALUES (@BN,@AN,@FileName, @FilePath,@LIB)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@BN", TextBox1.Text);
cmd.Parameters.AddWithValue("@AN", TextBox2.Text);
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "images/" + FileName);
cmd.Parameters.AddWithValue("@LIB", "YES");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
請注意,當您將此上傳到服務器時,您可能必須爲您的應用授予寫入您要保存到的文件夾的權限。 – peroija