0

Using ASP .Net MVC 4 Razor.

On my view page, I have HTML

    <div>
        <input type="file" id="multiFile" class="multi" name="multiFile" accept="jpg|png|gif|xls|xlsx|doc"/>
    </div>

I am using following scripts:

<script src="http://jquery-multifile-plugin.googlecode.com/svn/trunk/jquery.js" 
type="text/javascript"></script>
<script src="http://jquery-multifile-plugin.googlecode.com/svn/trunk/jquery.MultiFile.js" 
type="text/javascript"></script>

In my conroller, [HTTPPost] method:

        [HttpPost]
        public ActionResult Index(SampleModel model)
        {
            if(Request.Files.Count>0)
                HttpPostedFileBase uploads = Request.Files[0];

          //More codes here with model.............................
        }

I am following a codeproject tutorial for Multiple File Upload . And facing problem with HttpPostedFileBase. the **Error is : Embedded statement cannot be a declaration or labeled statement.

Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • Hmm, I'm not sure about the error, but in the controller method you need to have the uploaded file(s) as argument(s). Which mean something like this: `public ActionResult Index(SampleModel model, HttpPostedFileBase multiFile)`. Notice that the `id` from the `HTML` should match the name of the argument in the controller method! – Leron Mar 13 '14 at 10:56
  • i have tried. but `multiFile` is `null`. – Abdur Rahim Mar 13 '14 at 11:11
  • Well, in fact, it's not clear - is this a part of `Html.BeginForm` or just pure `HTML`. If the second check the `action` attribute. In fact check it anyways - do you get any data at all? – Leron Mar 13 '14 at 11:19
  • In my index action, when I debug, Request.Form["multiFile"] contains all uploaded filename. like `a.png,b.doc,c.xls`. but my Request.Files["multiFile"] still returns null. – Abdur Rahim Mar 13 '14 at 12:07

1 Answers1

0

The Html ihave used (only added multiple="multiple")

<div>
    <input type="file" multiple="multiple" id="multiFile" class="multi" name="multiFile" accept="jpg|png|gif|xls|xlsx|doc"/>
</div>

And in my controller:

[HttpPost]
    public ActionResult Index(SampleModel model)
    {

        string fileName1 = "";
        HttpPostedFileBase uploads ;

        if (Request.Files.Count > 0)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                //uploads[i] = new HttpPostedFileBase();
                uploads = Request.Files[i];
                fileName1 = Path.GetFileName(uploads.FileName);
                uploads.SaveAs(Server.MapPath(fileName1));
            }
        }

And I have uploaded all files!!!!

Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83