I am new to mvc and I have a problem. I was looking for an answer, and I could not find it, but I am very sure that something missed me. The problem is that I do not know how to get the file after uploading to the App_Data folder. I use the same code as in all forums:
For my viewing, I use this
@using (Html.BeginForm("Index", "Home", FormMethod.Post,
new { enctype="multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="submit" />
}
For my controller, I use this
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
}
My model
public class FileDescription
{
public int FileDescriptionId { get; set; }
public string Name { get; set; }
public string WebPath { get; set; }
public long Size { get; set; }
public DateTime DateCreated { get; set; }
}
I want to upload a file to the database, and then WebPath - a link to my file. I hope I made myself clear enough. Any help would be really appreciated. Thanks
source
share