文件上传功能
用的gridview显示数据,用RowCommand事件删除对应ID的数据。
现在想删除数据的同时删除 fileupload文件夹下对应的文件。
fileupload下的文件名存在 数据库中 ptah字段新手,求说的详细些,谢谢

解决方案 »

  1.   

    可以的,根据ID去数据库查path,根据path用FileInfo.Delete删除文件
      

  2.   

    传入要删除的文件路径,File.Delete("***")
      

  3.   

    是的 先删除存在的文件 再删除数据库的,
    参考如下代码//获取资源编号
    string fielid = context.Request["id"];
    string[] fileids = fielid.TrimEnd('|').Split('|');//用|分开各个资源编号
    Asiastar.NRBLL.File bll = new Asiastar.NRBLL.File();
    foreach( string guid in fileids ) //遍历编号集
    {
    if( bll.FN_SerchPathByFileId(new Guid(guid)) != null || bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows.Count > 0 )
    {
    for( int i = 0 ; i < bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows.Count ; i++ )
    {
    string path = bll.FN_SerchPathByFileId(new Guid(guid)).Tables[0].Rows[i]["FilePath"].ToString();  //定义变量接收数据库资源完整路径
    if( !Directory.Exists(path) )  //判断文件路径是存在
    {
    try
    {
    System.IO.File.Delete(path);  //如果路径存在 先删除服务器上的文件 在执行对数据库文件的删除操作
    } catch( Exception ex )
    {
    NR.Error.Log.LogType(ex.ToString());
    }
    NRBLL.File nbf = new Asiastar.NRBLL.File();
    if( nbf.FN_DeleteRes(new Guid(guid)) == true )
    {
    NR.Error.Log.LogType("删除资源成功!" + path);
    }
    else
    {
    context.Response.Write("1");
    NR.Error.Log.LogType("添加资源失败!" + path);
    }
    }
    }
    }
    }
    context.Response.Write("0");
      

  4.   

    NR.Error.Log.LogType("添加资源失败!" + path);这个是笔误 删除资源失败- - 
      

  5.   

    1;文件名是id吗?
    如果是话
    var path = Server.MapPath("~/fileupload/" + adId); //指定文件路径
    if(Directory.Exists(path)){ //该文件是否存在
    if (System.IO.Directory.GetFiles(path).Length > 0) //里面是否有内容 
    {
    try
    {
    System.IO.Directory.Delete(path, true); //干掉文件夹
    }
    catch
    {

    }
    }