1

Hi to all of you I am facing a problem on uploading a file and saving it into a folder here is my view

<div class="admin-empty-dashboard">
@using (Html.BeginForm("UploadResumes", "Employee", new { enctype = "multipart/form-data" }))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="icon">
        <i class="ion-ios-book-outline"></i>
    </div>

    <h4>Welcome @Model.FullName!</h4>
    <p>@ViewBag.Error</p>
    <div class="form-group bootstrap-fileinput-style-01">
        <label>Upload Resume</label>
        <input type="file" name="file" required="required" class="btn btn-primary" style="margin-left:265px" accept="application/msword,text/plain, application/pdf">
        <span class="font12 font-italic">** File must not bigger than 2MB</span>
    </div>

    <input type="submit" name="name" class="btn btn-primary" value="UploadResumes" />
}

</div>

and here is my controllers action problem is that it HttpPostedFileBase object value is always null and i ran Request.Files.Count it's also giving me zero where is problem?

public ActionResult UploadResumes(HttpPostedFileBase file)
{
    if (Session["UserID"] != null && Session["UserName"] != null && Session["EmployeeID"] != null)
    {
        int id = Convert.ToInt32(Session["EmployeeID"]);
        string[] allowedExtenssions = new string[] { ".pdf", ".doc", ".docx" };
        string cvPath = "~/cvs/Employee_cvs/";
        if (!Directory.Exists(Server.MapPath(cvPath)))
            Directory.CreateDirectory(Server.MapPath(cvPath));

        MyDbContext db=new MyDbContext();
        tblEmployee employee = (from s in db.tblEmployees where s.UserId == id select s).FirstOrDefault();

        // HttpPostedFileBase cv = Request.Files["CV"];
        if (file != null)
        {
            if (file.ContentLength > 0)
            {

                string ext = Path.GetExtension(file.FileName);
                if (allowedExtenssions.Contains(ext.ToLower()))
                {
                    string fileName = DateTime.Now.Ticks + ext;
                    string filePath = cvPath + fileName;
                    string serverPath = Server.MapPath(filePath);
                    file.SaveAs(serverPath);
                    employee.cvUrl = filePath;
                }

            }
            ViewBag.Error = "Some Error Occured";
        }

        return RedirectToAction("Dashboard");

    }
    else
    {
        return RedirectToAction("Login", "User");
    }
}
3
  • Possible duplicate of Why is MVC HttpPostedFileBase always null? Commented Aug 6, 2016 at 18:14
  • Dear Please solve my problem I shall be very thankful to all of you Commented Aug 6, 2016 at 19:24
  • Have you add [HttpPost] before your controller action? Commented Aug 7, 2016 at 7:15

3 Answers 3

1

Update like below and check

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

}

Also

[HttpPost]
public ActionResult UploadResumes(HttpPostedFileBase file)
{
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing your parameters on your controller to

public ActionResult UploadResumes(Model yourModel, HttpHostedFileBase file)

Comments

0

In most of my controllers that require a file for upload, are usually array based. Make you function parameters an array like this:

public ActionResult UploadResumes(HttpPostedFileBase[] files){
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.