如何在MVC中完成操作后从文件夹fileupload中删除excel文件 [英] How I delete excel file from folder fileupload after finish action in MVC

查看:88
本文介绍了如何在MVC中完成操作后从文件夹fileupload中删除excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传新的excel文件时遇到问题,因为我在完成操作后无法删除文件。



如何删除它?



这是我的代码: -

i have problem when i upload new excel file because i can't delete file after finish action.

how i delete it ?

this is my code :-

[HttpPost]
        public ActionResult Import(HttpPostedFileBase FileUpload)
        {
            bool fileChoosed = false;
            DataSet ds = new DataSet();
            if (FileUpload != null)
            {
                if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    string filename = FileUpload.FileName;
                    string targetpath = Server.MapPath("~/fileUpload/");

                    FileUpload.SaveAs(targetpath + filename);
                    
                    string pathToExcelFile = targetpath + filename;
                    var connectionString = "";
                    if (filename.EndsWith(".xls"))
                    {
                        connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
                    }
                    else if (filename.EndsWith(".xlsx"))
                    {
                        connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", pathToExcelFile);
                    }

                    OleDbConnection excelConnection = new OleDbConnection(connectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();

                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return null;
                    }

                    String[] excelSheets = new String[dt.Rows.Count];
                    int t = 0;
                    //excel data saves in temp file here.
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[t] = row["TABLE_NAME"].ToString();
                        t++;
                    }
                    OleDbConnection excelConnection1 = new OleDbConnection(connectionString);


                    string query = string.Format("Select * from[{0}]", excelSheets[0]);
                    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                    {
                        dataAdapter.Fill(ds);
                    }
                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string conn = ConfigurationManager.ConnectionStrings["WWMCONNDB"].ConnectionString;
                    SqlConnection con = new SqlConnection(conn);
                    // //if customer code found
                    string s1 = @"SELECT COUNT(*) FROM DATA_CUSTOMERS WHERE customer_id = @customer_id";
                    SqlCommand sCommand = new SqlCommand(s1, con);
                    sCommand.Parameters.AddWithValue("@customer_id", ds.Tables[0].Rows[i][11]);
                    con.Open();
                    int records = (int)sCommand.ExecuteScalar();
                    con.Close();
                    if (records == 0)
                    {
                        string s2 = @"SELECT COUNT(*) FROM DATA_NOTIFICATIONS WHERE notification_id = @notification_id";
                        SqlCommand sCommand2 = new SqlCommand(s2, con);
                        sCommand2.Parameters.AddWithValue("@notification_id", ds.Tables[0].Rows[i][1].ToString());
                        con.Open();
                        int records2 = (int)sCommand.ExecuteScalar();
                        con.Close();
                        if (records2 == 0)
                        {
                            string query = "Insert into DATA_CUSTOMERS(customer_id,customer_name,region_id,city_name,address,tele1,tele2) Values('" +
                            ds.Tables[0].Rows[i][11] + "','" + ds.Tables[0].Rows[i][6].ToString() +
                            "','" + ds.Tables[0].Rows[i][3] + "', '" +
                            ds.Tables[0].Rows[i][4].ToString() + "','" +
                            ds.Tables[0].Rows[i][5].ToString() + "','" +
                            ds.Tables[0].Rows[i][7].ToString() + "','" +
                            ds.Tables[0].Rows[i][8].ToString() + "'); insert into DATA_NOTIFICATIONS(notification_id,notificationtype_id,customer_id,problem_desc) Values('" +
                            ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][0] + "','" +
                            ds.Tables[0].Rows[i][11] + "','" +
                            ds.Tables[0].Rows[i][10].ToString() + "');";
                            con.Open();
                            SqlCommand cmd = new SqlCommand(query, con);
                            cmd.ExecuteNonQuery();
                            con.Close();
                            fileChoosed = true;

                        }
                    }
                }

            }


            return View(fileChoosed);
        }
    }





我的尝试:



如何从文件夹中删除excel文件



What I have tried:

How i delete excel file from folder

推荐答案

您的代码泄漏了资源。在Excel工作表和SQL连接的情况下,您创建一个Connection对象,但是您永远不会关闭并处理它们。



这也可能是你的原因所在无法删除该文件。由于您从不处理Connection对象,因此文件保持锁定状态。
Your code leaks resources. In both the case of the Excel sheet and your SQL connection, you create a Connection object but you never close and Dispose them.

This also goes to the probable reason you can't delete the file. Since you never Dispose the Connection object the file stays locked open.


这篇关于如何在MVC中完成操作后从文件夹fileupload中删除excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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