如标题!
Javascript:
public void DeleteImage_OnClick(object sender, EventArgs e) {
if (FileToDelete.Value != "" && FileToDelete.Value != "undefined") {
try {
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
System.IO.File.Delete(AppPath + CurrentImagesFolder.Value + "\\" + FileToDelete.Value);
ResultsMessage.Text = "Deleted: " + FileToDelete.Value;
} catch(Exception ex) {
ResultsMessage.Text = "There was an error.";
}
} else {
ResultsMessage.Text = NoFileToDeleteMessage;
}
DisplayImages();
}
从数据库取出文件名,加上硬盘的绝对路径,再删除.
参考代码:
private void DeletePic(int id)
{
string sqlstr="select pic from product where id=" +id.ToString();
OleDbConnection conn=new OleDbConnection(Application.Get("connstr").ToString());
conn.Open();
OleDbCommand cmd=new OleDbCommand(sqlstr,conn);
cmd.CommandType=CommandType.Text;
string picPath=((object)cmd.ExecuteScalar()).ToString();
if (picPath.Length!=0)
{
int i= picPath.LastIndexOf(@"\");
//取得文件名
string fileName =picPath.Substring(i);
picPath=Application.Get("uploadpath").ToString()+fileName;
if(File.Exists(picPath))
{
File.Delete(picPath);
}
}
sqlstr="select smallpic from product where id="+id.ToString();
cmd.CommandText=sqlstr;
cmd.CommandType=CommandType.Text;
picPath=((object)cmd.ExecuteScalar()).ToString();
if (picPath.Length!=0)
{
int i= picPath.LastIndexOf(@"\");
//取得文件名
string fileName =picPath.Substring(i);
picPath=Application.Get("uploadpath").ToString()+fileName;
if(File.Exists(picPath))
{
File.Delete(picPath);
}
}
conn.Close();
}
System.IO.File.Delete("c:\\temp\\file.bak")