Я читаю ответ , чтобы загрузить файл с помощью MVC. У меня уже есть файл после InputStream.Read
, но я не знаю, как его использовать для создания IEnumerable<MyModel>
, поэтому я могу отправить его в db с помощью EF. Файл является файлом CSV, я знаю структуру.
public class MyViewModel
{
[Required]
public HttpPostedFileBase File { get; set; }
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
byte[] uploadedFile = new byte[model.File.InputStream.Length];
model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
// now you could pass the byte array to your model and store wherever
// you intended to store it
return Content("Thanks for uploading the file");
}
Моя модель:
public class PriceViewModel
{
public int PriceId { get; set; }
public int? YearSelected { get; set; }
public int? WeekSelected { get; set; }
public int? StateSelected { get; set; }
}