1

I want to post a file to a controller/action in my Sitecore project via a "file" input tag.

<input value="choose file" type="file" id="bulk-upload"> 

I see examples, similar to below, online using forms. Is there a better/other way to do this?

@using (Html.BeginForm ("file", "upload", FormMethod.Post, new { enctype = "multipart/form-data" })) 
  • 2
    This is a pretty standard way. Is it causing problems for you? What are you looking to improve? – Corey Smith Oct 7 '16 at 17:15
6

In de HttpPost action method, capture your file by using a model object as parameter that has a property of the type HttpPostedFileBase with the same name as your input file field.

// The model
public class MyModel
{
    public HttpPostedFileBase UploadedFile { get; set; }
}

// The action method
[HttpPost]
public ActionResult MyAction(MyModel model)
{
    var file = model.UploadedFile;
}

In the view you posted, only the name attribute is missing to comply with this example code.

<input value="choose file" type="file" id="bulk-upload" name="UploadedFile"> 
| improve this answer | |
  • I don't think this answers the question. The OP asked if there are other better ways to post a file to a controller action. What you have written in your answer is the same way he's already using, isn't it? – Dmytro Shevchenko Oct 10 '16 at 7:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.