I need to add an upload function. A component on the page that allows you to upload a file that has a list of movies and information about the movies then display it to screen (webpage) and save to a database.
The part I need help with is parsing the contents of the uploaded csv file and saving the data to a sql server database and displaying the list of contents to the screen. I have created the upload function already see below:
This is an ASP.NET MVC 4 movie application.
Below is what I have so far:
View:
@using (Html.BeginForm("Upload", "Movies", FormMethod.Post, new {enctype="multipart/form-data"}))
{
<input type="file" name="File" id="File" />
<input type="submit" name="Submit" value="Upload" />
<span>@ViewBag.Message</span>
}
Controller:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase File )
{
// Verify that the user selected a file
if (File != null && File.ContentLength > 0)
{
// extract only the filename
var fileName = Path.GetFileName(File.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
File.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
Model (database):
public class Movie
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
//[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
public string Genre { get; set; }
[Range(1,100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
public bool IsCompleted { get; set; }
[StringLength(5)]
public string Rating { get; set; }
public string EnterUrl { get; set; }
//public HttpPostedFileBase Document { get; set; }
}
public class MovieDBContext : DbContext
{
public DbSet<Movie> Movies { get; set; }
}