Moti Mashiah
asked on
asp.net mvc input file
Hi guys,
I'm trying to do something really simple that I did before and for some reason its not working.
I'm trying to upload image through html and save it, but when I upload it from the view and get it in the controller it shows NULL anytime.
Here is how it works:
I initiate Index page then I have this html inside my Index page:
After I click submit I send the file to the controller:
My controller:
Issue: the controller get the value NULL - what am I doing wrong here as I can't see anything wrong.
Thanks,
I'm trying to do something really simple that I did before and for some reason its not working.
I'm trying to upload image through html and save it, but when I upload it from the view and get it in the controller it shows NULL anytime.
Here is how it works:
I initiate Index page then I have this html inside my Index page:
<h2>Basic File Upload</h2>
@using (Html.BeginForm("FileUpload", "Uploadpictures", FormMethod.Post))
{
<div class="row">
<div class="col-sm-4 form-group">
<input type="file" name="uploadFile" id="upload" />
<input type="submit" name="submit" value="Upload Image" />
</div>
</div>
<br><br>
@ViewBag.Message
}
After I click submit I send the file to the controller:
My controller:
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Images"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
return RedirectToAction("Index", "Uploadpictures");
}
Issue: the controller get the value NULL - what am I doing wrong here as I can't see anything wrong.
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Actually I tried the enctype before and it didn't work for some reason.