C# 找不到资源HTTP 404错误| HttpPost | HomeController.cs
我正在开发我的第一个Web应用程序,我遇到了一个无法解决的问题。 我正在使用ASP.NET Web应用程序(.NET Framework)MVC,我想在我的网站上获得一个按钮,用户可以在其中上载excel文件,并将excel文件传递到(本地)SQL数据库。我不能让它工作,我看了10个youtube视频和20个不同的网站,但我不能让它工作 这是HomeController.cs中的代码:C# 找不到资源HTTP 404错误| HttpPost | HomeController.cs,c#,html,excel,database,C#,Html,Excel,Database,我正在开发我的第一个Web应用程序,我遇到了一个无法解决的问题。 我正在使用ASP.NET Web应用程序(.NET Framework)MVC,我想在我的网站上获得一个按钮,用户可以在其中上载excel文件,并将excel文件传递到(本地)SQL数据库。我不能让它工作,我看了10个youtube视频和20个不同的网站,但我不能让它工作 这是HomeController.cs中的代码: using System; using System.Collections.Generic; using S
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
namespace WebApplication5.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
DataSet ds = new DataSet();
if (Request.Files["file"].ContentLength > 0)
{
string fileExtension =
System.IO.Path.GetExtension(Request.Files["file"].FileName);
if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["file"].SaveAs(fileLocation);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
//connection String for xls file format.
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
//connection String for xlsx file format.
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
//Create Connection to Excel work book and add oledb namespace
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
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(excelConnectionString);
string query = string.Format("Select * from [{0}]", excelSheets[0]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
dataAdapter.Fill(ds);
}
}
if (fileExtension.ToString().ToLower().Equals(".xml"))
{
string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["FileUpload"].SaveAs(fileLocation);
XmlTextReader xmlreader = new XmlTextReader(fileLocation);
// DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
SqlConnection con = new SqlConnection(conn);
string query = "Insert into Person(Name,Email,Mobile) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "')";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统配置;
使用系统数据;
使用System.Data.OleDb;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用System.Xml;
命名空间WebApplication5.控制器
{
公共类HomeController:控制器
{
[HttpPost]
公共操作结果索引(HttpPostedFileBase文件)
{
数据集ds=新数据集();
if(Request.Files[“file”].ContentLength>0)
{
字符串文件扩展名=
System.IO.Path.GetExtension(Request.Files[“file”].FileName);
如果(文件扩展名==“.xls”| |文件扩展名==“.xlsx”)
{
字符串fileLocation=Server.MapPath(“~/Content/”)+Request.Files[“file”].FileName;
if(System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files[“file”].SaveAs(文件位置);
字符串excelConnectionString=string.Empty;
excelConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+fileLocation+”“扩展属性=\”Excel 12.0;HDR=Yes;IMEX=2\”;
//xls文件格式的连接字符串。
如果(文件扩展名=“.xls”)
{
excelConnectionString=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+fileLocation+”“扩展属性=\”Excel 8.0;HDR=Yes;IMEX=2\”;
}
//xlsx文件格式的连接字符串。
else if(文件扩展名=“.xlsx”)
{
excelConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+fileLocation+”“扩展属性=\”Excel 12.0;HDR=Yes;IMEX=2\”;
}
//创建到Excel工作簿的连接并添加oledb命名空间
OleDbConnection excelConnection=新的OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt=新的DataTable();
dt=excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
如果(dt==null)
{
返回null;
}
String[]excelSheets=新字符串[dt.Rows.Count];
int t=0;
//excel数据保存在此处的临时文件中。
foreach(数据行中的数据行)
{
excelSheets[t]=行[“表名称”]。ToString();
t++;
}
OLEDB连接excelConnection1=新的OLEDB连接(excelConnectionString);
string query=string.Format(“Select*from[{0}]”,excelSheets[0]);
使用(OleDbDataAdapter dataAdapter=新的OleDbDataAdapter(查询,excelConnection1))
{
dataAdapter.Fill(ds);
}
}
if(fileExtension.ToString().ToLower().Equals(“.xml”))
{
字符串fileLocation=Server.MapPath(“~/Content/”)+Request.Files[“FileUpload”].FileName;
if(System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files[“FileUpload”].SaveAs(fileLocation);
XmlTextReader xmlreader=新的XmlTextReader(文件位置);
//数据集ds=新数据集();
ReadXml(xmlreader);
xmlreader.Close();
}
对于(int i=0;i
这是我的Index.cshtml中的代码(我认为您不需要这个,但安全总比抱歉好)
索引
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
问题是我
<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Index()
{
return View();
}