そのため、ファイルアップロード機能はしばらく機能していましたが、突然機能しなくなり、新しいプロジェクトでテストして古いバージョンのコードに戻した後でも問題が残りました。
以下は私のアップロード機能のコードです。
public void EnsureDirectoriesExist()
{
string currentuser = "admin";
//string currentuser = (string)(Session["uname"]);
// if the \pix directory doesn't exist - create it.
if (!System.IO.Directory.Exists(Server.MapPath(@"~/userimages/" + currentuser + "/displaypicture/")))
{
System.IO.Directory.CreateDirectory(Server.MapPath(@"~/userimages/" + currentuser + "/displaypicture/"));
}
}
protected void uploadbtn_Click(object sender, EventArgs e)
{
string currentuser = "admin";
//string currentuser = (string)(Session["uname"]);
if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".png")
{
// create posted file
// make sure we have a place for the file in the directory structure
EnsureDirectoriesExist();
String filePath = Server.MapPath(@"~/userimages/" + currentuser + "/displaypicture/" + FileUpload1.FileName);
String filePath2 = ("~/userimages/" + currentuser + "/displaypicture/" + FileUpload1.FileName);
FileUpload1.SaveAs(filePath);
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["myConnectionString"].ToString());
string mySQL, mySQL2;
mySQL2 = "DELETE FROM displaypicture WHERE username='" + currentuser + "'";
mySQL = "INSERT INTO displaypicture(username,path) VALUES ('" + currentuser + "','" + filePath2 + "')";
conn.Open();
SqlCommand cmdAdd2 = new SqlCommand(mySQL2, conn);
SqlCommand cmdAdd = new SqlCommand(mySQL, conn);
cmdAdd2.ExecuteNonQuery();
cmdAdd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Upload successful!");
}
else
{
MessageBox.Show("Upload has failed. Please check file format.");
}
}
したがって、基本的には、if 関数が正しくないように見えるため、else 関数を使用します。しかし、突然機能しなくなるまで、約 2 週間は機能したと断言できたはずです。また、if関数を次のように切り替えました。
if(currentuser.equals("admin")){
}
if else 要件ステートメントが正しいかどうかをテストし、それを行ったときに if 関数が実行されますが、ディレクトリのみが作成されますが、ファイルはディレクトリにアップロードされません。