-1

I am trying to upload files using jquery ajax. I have done same by referring this http://powerdotnetcore.com/asp-net-mvc/asp-net-mvc-simple-ajax-file-upload-using-jquery. It works good with single file.

But I have to upload multiple files, even I have model with one to many relationship, and I have to save model values before uploading file.

If done with full postback it works well. But if I serialize the form it not works.

Please help to resolve the same issue.

Is there any way without using any plugins to resolve same? Is any alternative for form serialization?

Vikas
  • 127
  • 10

1 Answers1

-1

use formData parametre and pass values through it.

var input = $('#input');
$('#fu-my-simple-upload').fileupload({
                url: '/File/UploadFile',
                dataType: 'json',
                formData : {id: input.val()},
                add: function (e, data) {
                    jqXHRData = data
                },
                done: function (event, data) {
                    if (data.result.isUploaded) {

                    }
                    else {

                    }
                    alert(data.result.message);
                },
                fail: function (event, data) {
                    if (data.files[0].error) {
                        alert(data.files[0].error);
                    }
                }
            });

also in controller add parameter name like this.

    public virtual ActionResult UploadFile(string id)
    {
     IEnumerable<HttpPostedFileBase> myFile = Request.Files["MyFile"];
     foreach(var file in myfile)
     {
     // save file one by one
     }        
     // now put your code
    }

in html input field use this...

<input type="file" multiple="" name="myfile[]">

Hope this helps...

C M
  • 684
  • 9
  • 22
  • Thank you dude it works fine. But if I have attach multiple file how can I do same? I am working on asp.net mvc4. – Vikas Oct 18 '13 at 12:22