Can someone help me out here. I've been following tutorials on how to use fileupload using MVC 5, but the file keeps failing to upload.
I have an Uploads folder in my App_Data folder where the files should be getting saved to. In my controller I have this:
using System.IO;
namespace [project_name].Controllers
public class [controller_name]: Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.Message = "";
try
{
// Verify that the user selected a file
if (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);
}
// redirect back to the index action to show the form once again
ViewBag.Message = "File Upload Successful";
return RedirectToAction("Index");
}
catch(Exception ex)
{
ViewBag.Message = ex.Message;
return View();
}
}
}
In my view I have this:
@{
ViewBag.Title = "File Upload";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-md-4">
@using (Html.BeginForm("Index", "[controller_name]", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@ViewBag.Message<br />
@Html.TextBox("file", "", new { type = "file" })
<input type="submit" value="Upload" />
}
</div>
</div>
<br />
So, what am I missing? This project is still in development and I'm using Visual Studio 2017, so this is still using localhost
for debugging. When I used breakpoints, it shows file
is still null
. The error I get form the catch block is "Object reference not set to an instance of an object.".
Anyone have any ideas as to what I've done wrong here?
UPDATE: I tried changing the name of the action to this:
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult UploadFile() { return View(); }
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
ViewBag.Message = "";
try
{
// Verify that the user selected a file
if (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);
}
// redirect back to the index action to show the form once again
ViewBag.Message = "File Upload Successful";
return RedirectToAction("Index");
}
catch(Exception ex)
{
ViewBag.Message = ex.Message;
return View();
}
}
I added the view, but when I tried again, I had the same problem.