我正在阅读此答案以使用MVC上传文件。我已经在a之后有了文件,InputStream.Read但是不知道如何使用它来创建文件,IEnumerable<MyModel>因此我可以使用EF将其发送到db。该文件是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; }
}
九州编程
相关分类