我需要这方面的帮助,我正在将文件目录插入数据库,但它没有考虑数据库中的 txtStoryTitle.Text,例如,如果我在 txtStoryTitle 中键入 HelloWorld。它在数据库中显示为 Images/Story//(filename) 而不是 Images/Story/HelloWorld/(filename)。我正在使用 MySQL(工作台)。
请给我对此的建议/解决方案,提前致谢!
部分代码如下:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
EnsureDirectoriesExist();
String filepathImage = (@"Images/Story/" + txtStoryTitle.Text + "/" + e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath(filepathImage));
Session["filepathImage"] = filepathImage;
}
public void EnsureDirectoriesExist()
{
if (!System.IO.Directory.Exists(Server.MapPath(@"Images/Story/" + txtStoryTitle.Text + "/")))
{
System.IO.Directory.CreateDirectory(Server.MapPath(@"Images/Story/" + txtStoryTitle.Text + "/"));
}
}
protected void btnDone_Click(object sender, EventArgs e)
{
if (Session["filepathImage"] != null)
{
string filepathImage = Session["filepathImage"] as string;
act.ActivityName = dropListActivity.SelectedItem.Text;
act.Title = txtStoryTitle.Text;
act.FileURL = filepathImage;
daoStory.Insert(act);
daoStory.Save();
}
最佳答案
根据您的代码..文件路径为“Images/Story/”+ txtStoryTitle.Text +“/”+ e.FileName” 提供 txtStoryTitle.Text 后,将其保存为“Images/Story//FileName”.. 那么这意味着 txtStoryTitle.Text 不包含任何文本..
如果是在 .Net 中,请确保将 txtStoryTitle 文本框的 autopostback 属性设置为 true。 如果它已经是真的,那么尝试找出为什么这个文本框不抵抗它的状态。
关于C#如何插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16580419/