csv ファイルをアップロードし、MVC3 を使用して有能な CSVHelper を実装しようとしています。
https://github.com/JoshClose/CsvHelper
ファイルのアップロードでこれを使用する例は見つかりませんでした。基本的に、CSV ファイルを取得してエンティティ オブジェクトにマップし、DB に保存する必要があります。ここに私のエンティティがあります:
public class SurveyEmailListModels
{
[Key]
public int SurveyEmailListId { get; set; }
[CsvField(Index = 0)]
public int ProgramId { get; set; }
[CsvField(Index = 1)]
public virtual SurveyProgramModels SurveyProgramModels { get; set; }
[CsvField(Index = 2)]
public string SurveyEmailAddress { get; set; }
[CsvField(Index = 3)]
public bool SurveyResponded { get; set; }
}
アップロード ハンドラ:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file, SurveyEmailListModels surveyemaillistmodels, int id)
{
if (file != null && file.ContentLength > 0)
{
// Collect file and place into directory for source file download
var appData = Server.MapPath("~/csv/");
var filename = Path.Combine(appData, Path.GetFileName(file.FileName));
file.SaveAs(filename);
// surveyemaillistmodels.SurveyEmailAddress = "test@test.com";
// surveyemaillistmodels.SurveyResponded = true;
// surveyemaillistmodels.ProgramId = id;
db.SurveyEmailListModels.Add(surveyemaillistmodels);
db.SaveChanges();
return Content(filename);
}
return Json(true);
}
CSV ファイルをループして DB に保存する方法がわかりません。誰か例がありますか?