嗨我有一個問題即時上傳圖片到圖片都包含在〜/用戶數據/用戶名/ uploadedimage/image.jpg的陌生的字符串格式+路徑從文件上傳
我的項目路徑我用下面的方法上傳並將圖片的路徑存儲在我的數據庫中。
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
string theUserId = Session["UserID"].ToString();
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
cn.Open();
//string filename = Path.GetFileName(FileUploadControl.FileName);
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(fileuploadpath);
StatusLabel.Text = "Upload status: File uploaded!";
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
我發現什麼是心不是我的項目目錄的路徑,該路徑的一些奇怪的事情:
這裏是我的數據庫的一個片段的第一個idPictures = 1是正確的路徑我需要的名字。
idPictures = 2是fileupload插入到我的數據庫中的那個?
我怎樣才能得到它,因此它會給出一個路徑名這樣的:
~/userdata/2/uploadedimage/batman-for-facebook.jpg
編輯:
如果我試試這個:
string fileuploadpath = ("~/userdata/"+theUserId+"/uploadedimage/")+Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(fileuploadpath);
StatusLabel.Text = "Upload status: File uploaded!";
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('"+theUserId+"','"+fileuploadpath+"')", cn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
我得到的錯誤:
檔案cou ld不會被上傳。發生以下錯誤:SaveAs方法被配置爲需要一個根路徑,並且路徑'〜/ userdata/1/uploadedimage/holypally.jpg'沒有根。
呃,也許這樣的側隙因爲idpictures 2缺少它們,但爲什麼它不像第一個圖片路徑那樣保存,而是它給出了一個完整的路徑 – 2011-03-25 11:57:02
@Garrith:你調用'Server.MapPath'並插入結果作爲'picturepath'的值。你確定你明白'Server.MapPath'的作用嗎? – 2011-03-25 11:58:07
不,我不想要翻譯的路徑和是的,我理解參數化的SQL語句,但ID喜歡先看到,如果我可以得到正確的路徑名稱,然後擔心反斜槓 – 2011-03-25 12:00:28