Как удалить файл excel из папки fileupload после завершения действия в MVC
у меня есть проблема, когда я загружаю новый файл excel, потому что я не могу удалить файл после завершения действия.
как я его удаляю ?
это мой код :-
[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 из папки