如何从asp.net中的文件夹中删除上传的文件 [英] how to delete the uploaded file from the folder in asp.net

查看:72
本文介绍了如何从asp.net中的文件夹中删除上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中上传了一个文件,该文件也包含在asp.net网站页面的文件夹中。

现在我要从数据库和文件夹中删除该文件也。我已经从数据库中成功删除了

文件,但文件没有从网站的文件夹中删除。

所以请告诉我正确的答案。

整个细节如下: -



首先我上传了一个文件(无论是图像,文件等)你可以

了解该文件也将转到该文件夹​​。这意味着该文件将转到数据库(sql server)并转到该网站的文件夹。



现在如果我要删除该文件,那么它将从数据库(sql server)中删除

但不是从网站的文件夹中删除(asp。 net).Folder名称如(图像,上传等)



这是我的c#代码,用于从数据库和文件夹中删除文件。文件夹名称为您可以在代码和数据库表名称中看到的上传是emp,因此您现在可以理解该文件正在从数据库表emp中删除,但不能从Uploads文件夹中删除。



代码如下: -



I had uploaded a file in the database and that file is also contains by a folder of the website page in asp.net.
Now i want to delete the file from the database and the folder also. I have successfully deleted the
file from the database but file is not deleting from the folder of the website.
so tell me the proper answer.
The whole detail is given below:-

Firstly I had uploaded a file(whether it is image,word file etc.)Now u can
understand that file will also go to the folder .It mean to say that file will go the database(sql server) and also go to the folder of the website.

Now If i want to delete that file then it is deleting from the databse(sql server)
but not to deleting from the folder of the website(asp.net).Folder name like (image,upload etc.)

Here is my c# code for delete a file from the database and the folder also.The folder name is "Uploads" which you can see in the code and the databse table name is "emp" so now you can understand that file is deleting from the database table "emp" but not deleting from the folder "Uploads".

Code is given below:-

protected void btndelete_Click(object sender, EventArgs e)
    {
 string connectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
        
          string filename = FileUpload1.FileName;
          FileUpload1.PostedFile.Equals(Server.MapPath("~\\Uploads\\" + filename));
          string path = "~\\Uploads\\" + filename;

       SqlConnection cnn = new SqlConnection(connectionString);
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "delete from emp ";
            
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cnn;
            cmd.ExecuteNonQuery();

          if (File.Exists(path))
            {
                File.Delete(path);
                 
            }

           
            cnn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
           lblinfo2.Text = "";
           lblinfo2.Text= " Deleted Successfully ";
        }

    }

推荐答案

请访问以下链接



带进度条的ASP.NET文件上传 [ ^ ]
Please visit following link

ASP.NET File Upload with Progress Bar[^]


参考从服务器目录中删除文件


在最后一个块写这个

in finally block write this
if (File.Exists(path))
            {
                File.Delete(path);

            }


这篇关于如何从asp.net中的文件夹中删除上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆