Link to home
Start Free TrialLog in
Avatar of Moti Mashiah
Moti MashiahFlag for Canada

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:
<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
}

Open in new window


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");
        }

Open in new window


Issue: the controller get the value NULL - what am I doing wrong here as I can't see anything wrong.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Moti Mashiah

ASKER

Thank you this is solved my problem.

Actually I tried the enctype before and it didn't work for some reason.