![](/img/trans.png)
[英]Why is my input type="file" always returning null to the viewmodel in ASP.NET MVC and Entity Framework
[英]Why is my csv file path always returning null?
我目前正在開發一個ASP.NET MVC項目,用戶可以在其中將csv和excel記錄插入數據庫中,然后進行編輯。 我已經完成了excel部分,並且工作出色。 但是另一方面,我的csv版本甚至無法獲取csv文件路徑,並且始終返回null。 我嘗試了不同的方法,但似乎都沒有用。
我的願望是保持excel版本和csv版本相似,因此我在兩者上都使用了httpPost,但由於某種原因,它無法獲取csv文件路徑。 有人可以看到我在做什么嗎?
這是我的家庭控制器視圖,應該發送文件路徑:
@{
ViewBag.Title = "Index";
}
<h1>Index</h1>
@using (Html.BeginForm("Import", "CSVImport", FormMethod.Post, new {enctype="multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
Select a file <input type="file" name="file" />
<input type="submit" value="Upload CSV" />
</fieldset>
}
這是我的csv進口商:
using System.Data.Common;
using System.Text;
using System.Data.ProviderBase;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using Microsoft.VisualBasic.FileIO;
namespace BFProj2.Controllers
{
public class CSVImportController : Controller
{
// GET: CSVImport
//public static DataTable GetDataTabletFromCSVFile(string csv_file_path)
[HttpPost]
public ActionResult Import(HttpPostedFileBase csv_file_path)
{
DataTable csvData = new DataTable();
if (csv_file_path != null && Request.Files["csv_file_path"].ContentLength > 0)
{
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path.InputStream))
{
//Solution to HDR problem: When getting to the checkbox part of the application, just keep the HDR unchecked.
csvReader.SetDelimiters(new string[] { "," });
//Can be true depending on if the csv document rows are enclosed in quotes or not. Delimiters are set as either ; or , depending on weather the csv columns are split with ; or ,
csvReader.HasFieldsEnclosedInQuotes = false;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn Titel = new DataColumn(column);
Titel.AllowDBNull = true;
csvData.Columns.Add(Titel);
DataColumn LastName = new DataColumn(column);
LastName.AllowDBNull = true;
csvData.Columns.Add(LastName);
DataColumn AbstrNum = new DataColumn(column);
AbstrNum.AllowDBNull = true;
csvData.Columns.Add(AbstrNum);
DataColumn PosterTitel = new DataColumn(column);
PosterTitel.AllowDBNull = true;
csvData.Columns.Add(PosterTitel);
DataColumn Workshop = new DataColumn(column);
Workshop.AllowDBNull = true;
csvData.Columns.Add(Workshop);
DataColumn Keywords = new DataColumn(column);
Keywords.AllowDBNull = true;
csvData.Columns.Add(Keywords);
DataColumn Institution = new DataColumn(column);
Institution.AllowDBNull = true;
csvData.Columns.Add(Institution);
DataColumn CollabEmail = new DataColumn(column);
CollabEmail.AllowDBNull = true;
csvData.Columns.Add(CollabEmail);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
csvData = System.Data.Common.DbProviderFactories.GetFactoryClasses();
ViewData.Model = csvData.AsEnumerable();
}
}
catch (Exception ex)
{
}
}
return View();
}
}
}
我從中獲取了一些原始資料: http : //www.morgantechspace.com/2013/10/import-csv-file-into-sql-server-using.html
我還沒有放入大容量復制部分,它只是一個DataTable,但我想制作它,以便它在控制器中執行所有操作,然后再次返回到homecontroller視圖,而不是將其放置在其他位置。
順便說一句,我對stackoverflow來說還很陌生,所以如果有其他問題我在做錯了,請問我需要改變它的指導。
發布文件時, <input>
的名稱用作Request.Files
的鍵以及action方法的參數名,因此它們需要匹配。 就您而言,您具有<input name="file">
但參數名稱csv_file_path
。 因此,您需要重命名一個或另一個:
<input name="file">
public ActionResult Import(HttpPostedFileBase file)
{
if (file != null && Request.Files["file"].ContentLength > 0)
...
}
要么
<input name="csv_file_path">
public ActionResult Import(HttpPostedFileBase csv_file_path)
{
if (csv_file_path != null && Request.Files["csv_file_path"].ContentLength > 0)
...
}
請注意,您也不需要使用Request.Files
-HttpPostedFileBase具有您需要的內容:
if (csv_file_path != null && csv_file_path.ContentLength > 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.