I'm trying to put together a page whereby a user can upload a file, and it goes to a database.
I'm following a tutorial, and my controller method so far looks like this:
public ActionResult Index()
{
ViewData["Message"] = "File Upload";
foreach (string upload in Request.Files)
{
if (!Request.Files[upload].HasFile()) continue;
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
string filename = Path.GetFileName(Request.Files[upload].FileName);
Request.Files[upload].SaveAs(Path.Combine(path, filename));
}
return View();
}
Here is also an example of what my view looks like:
<p>
<% using (Html.BeginForm("", "home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
<input type="file" name="FileUpload1" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
<% } %>
</p>
I'm currently getting two compile errors however:
- 'System.Web.HttpPostedFileBase' does not contain a definition for 'HasFile' and no extension method 'HasFile' accepting a first argument of type 'System.Web.HttpPostedFileBase' could be found (are you missing a using directive or an assembly reference?)
- The name 'Path' does not exist in the current context
Here is also an example of what I'm using for namespaces in the controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
I'd be very grateful if anyone could point me in the right direction to fixing this error.