1

I need to upload a file and I have found several ways to do so. The best way I thought to do it was according to this blog: Blog

I also found a usefull post here: stackoverflow topic

But when I try it, it fails. I get the following error in visual studio:

Sequence contains more than one element, with not much more to go further on.

My code looks like this:

Controller:

public PartialViewResult Index(parameterlist)
    {
        var model = new Model();


        return PartialView(model);
    }

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0) 
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);

            var path =         Path.Combine(Server.MapPath(@"Path"), fileName);
            file.SaveAs(path);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");            
    }

View:

            <div class="span6">                                
            @using (Html.BeginForm("null", "null", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                <input type="file" name="file" />
                <input type="submit" value="OK" />
            }
        </div>

So the moment this partialview gets called it goes to the actionResult method, ofcourse there is no file selected because the view hasn't been opened yet. There are some other textboxes and dropdowns in the view. But nothing special, anyone has an idea on what it is that I'm doing wrong? I've got the feeling I'm missing something crucial ...

dove
  • 20,469
  • 14
  • 82
  • 108
el shorty
  • 237
  • 2
  • 5
  • 14
  • Can you post the stacktrace or the literal exception message you get? – Rob Oct 05 '12 at 11:46
  • Everything looks fine, however if you wish to avoid this implemententation you could use the Microsoft.Web.Helpers Namespace because it contains a FileUpload class that can help you. Ex: http://msdn.microsoft.com/en-us/library/microsoft.web.helpers%28v=vs.99%29.aspx – Freeman Oct 05 '12 at 11:53
  • Ok I found why I got this exception, I needed to pass my parameterlist with when I did RedirectToAction("Index") However now the page loads but when I try to upload a file it doesn't upload instead it executes a function of another button on the page – el shorty Oct 05 '12 at 12:03

2 Answers2

1

To avoid executing the wrong function, you should specify the Controller and Action in Html.BeginForm rather than the null values you have there.

@using (Html.BeginForm("Index", "YourController", FormMethod.Post, ...
dove
  • 20,469
  • 14
  • 82
  • 108
  • I figured out now that the problem in my view is that I have a form in a form. I need to submit the uploaded image together with other details. But before those details can be entered the file has to be uploaded first – el shorty Oct 05 '12 at 12:28
  • meant to mention that too. looks like you got them separated and sorted anyway. – dove Oct 05 '12 at 13:02
0

I tried merging them to one form, so my form now looks like this:

@using (Ajax.BeginForm("Method", "Controller",new { enctype = "multipart/form-data" }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "Method" }, new { @class = "css", id = "formname" }))
{
   Some divs and stuff

   <input type="file" name="file" class="btn btn-inverse" />
   </div>

the function looks like this:

[HttpPost]
        public void _SaveDocument(Model model, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath(@"Path"), fileName);
                file.SaveAs(path);

                var command = new command()
                {
                    Model = model
                };
                commandDispatcher.Dispatch(command);
            }            
        }

But file never gets added, it is always null

el shorty
  • 237
  • 2
  • 5
  • 14
  • you cannot upload file using ajax. to do this you'd need to look at special components such as Ajax Upload or Uploadify. – dove Oct 05 '12 at 13:43