1

I want to upload multiple files including Word Documents, Pdf and Images. I need to use a single input for files because we don't know how many files will be uploaded.

My code is this but I have problem that I can't send files to server side.

Controller Code :

public ActionResult Create([Bind(Include = "Id,Content")] Message message, IEnumerable<HttpPostedFileBase> files)
{
     if (ModelState.IsValid)
     {
         // Object save logic
         SaveFiles(message,files);
         return RedirectToAction("Index");
     }
     return View(message);
}

Part of view code :

<form name="registration"  action="">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <label for="Content" >Content:</label>
            <textarea class="form-control compose_content" rows="5" id="Content" name="content"></textarea>

            <div class="fileupload">
                      Attachment :  

                    <input id="files" name="files" type="file" multiple />
            </div>
        </div>
    <button type="submit" class="btn btn-default compose_btn" >Send Message</button>
</form>

I don't have problem with saving files and saving the object. only problem : files list is null.

2
  • Show your <form> element Commented Mar 18, 2017 at 7:18
  • I added that in question. Commented Mar 18, 2017 at 7:26

1 Answer 1

3

I order to upload files, your form needs to include the enctype= multipart/form-data attribute.

<form name="registration"  action="" enctype= "multipart/form-data">

or better

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

and I strongly recommend you pass a model to the view and use the strongly typed HtmlHelper methods to create your html for the properties of your model.

Sign up to request clarification or add additional context in comments.

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.