Shahzaib Ali Khan
Shahzaib Ali Khan

Reputation: 75

File Uploading In MVC 3 is always null

I wanted create a form where user will edit there profile and upload an image if they want, Here is my View

//Form String @using(Html.BeginForm("EditProfile","Employee",FormMethod.Post, new { enctype = "multipart/form-data " }))

//Input HTML <input type="file" name="file" id="file"/>

//Submit<input type="submit" name="SubmitBtn" value="SaveProfile" /> <input type="submit" name="SubmitBtn" value="Cancel" />

Now My Controller

    //Profile Modification
    [Authorize]
    [HttpPost]
    public ActionResult EditProfile(employer model, string SubmitBtn,HttpFileCollection file)
    {
        switch (SubmitBtn)
        {
            case "SaveProfile":
                try
                {
                    if (ModelState.IsValid)
                    {
                          //the file is always null
                        if (file != null)
                        {
                             //Function I want to Apply on file
                             model.logoname = logouploded(file);  
                            return RedirectToAction("Profile", "Employer");
                        }
                        return RedirectToAction("EditProfile");
                    }
                    //ChangeEmployeeProfile(model);

                }
                catch (Exception ex)
                {
                    ViewBag.warn = ex.Message;
                    return View(model);
                }
                break;
            case "Cancel":
                return RedirectToAction("index");
        }
        return View(model);
    }

Now no matter what file I upload file is always coming null I have also tried HttpFileCollectionBase as parameter in Action Function still file was null

Just to mention Model of Employee contains only image file name because I only want to save image name in database.

Upvotes: 7

Views: 2536

Answers (1)

Peter Kiss
Peter Kiss

Reputation: 9319

Change HttpFileCollection type to HttpPostedFileBase.

Upvotes: 12

Related Questions