Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
@using (Html.BeginForm("UploadAction1", "Media", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmID" }))
{}


C#
$(document).ready(function () {
        $('#fileupload').fileupload({
            dataType: 'json',
            url: '/Media/UploadFiles1',
            autoUpload: true,
            singleFileUploads: false,
            done: function (e, data) {

            }
        }).on('fileuploadprogressall', function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('.progress .progress-bar').css('width', progress + '%');
        });
});



I am using fileupload plugin , where i want to post formdata along with image.
something like this should work

C#
data: $('#frmID').serialize()
,

I am getting image values using Request.Files but I am unable to post any data.
https://github.com/blueimp/jQuery-File-Upload/wiki/Options[^]
Posted
Updated 7-Oct-15 5:54am
v6
Comments
Richard Deeming 7-Oct-15 10:20am    
Which of the dozens of fileupload plugins are you using?
Member 9129971 7-Oct-15 11:54am    
https://github.com/blueimp/jQuery-File-Upload/wiki/Options...this one
Richard Deeming 7-Oct-15 12:06pm    
According to that page, the property is called formData, not data:
formData: $("#frmID").serialize()
Member 9129971 8-Oct-15 3:11am    
tried formData: $("#frmID").serialize() but still not able to get form values,(same problem)
Member 9129971 7-Oct-15 11:53am    
https://github.com/blueimp/jQuery-File-Upload/wiki/Options...this one

1 solution

Of course, this plugin is only for the file upload, and not the data along with it.
Quote:
The jQuery File Upload Plugin consists of a basic version (jquery.fileupload.js) providing the File Upload API.

It's okay but why to go for 3rd party solution when HTML5 provides the file input ?
HTML
<input type="file" id="fileUpload" name="fileUpload" />

Here, type="file", defines a file-select field and a "Browse..." button (for file uploads).

Now let's make a demo.
HTML:
HTML
<!--You can change it to @Html.BeginForm -->
<form id="myForm" action="@Url.Action('UploadAction1', 'Media')" enctype="multipart/form-data">
    @Html.TextBoxFor(m => m.Name)
    <input type="file" id="fileUpload" name="fileUpload" />
</form>

Now the jQuery,
JavaScript
$("#fileUpload").change(function() {
    $("#myForm").submit();  // meaning whenever you select the file and press OK/Open, form will be submitted.
});

You can refer this documentation [^] for showing the file upload progress.

And finally the Action Method
[HttpPost]
C#
public ActionResult UploadAction1()
{
     if (Request.Files.Count > 0)
     {
         var file = Request.Files[0];

         if (file != null && file.ContentLength > 0)
         {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
            file.SaveAs(path);
         }
     }

     return RedirectToAction("Index");
 }


-KR
 
Share this answer
 
Comments
Member 9129971 9-Oct-15 3:49am    
I tried this solution but I need to upload multiple files at a time. Here when I post multiple files by using
<input type="file" id="fileUpload" name="fileUpload" multiple/>..then it only selects first file.For example if I upload 3 files at a time (file1,file2,file3) , then Request.file will contain 3 items (file1,file1,file1).How can I resolve this issue?
Krunal Rohit 12-Oct-15 10:01am    
you can use a loop. Iterate through Request.Files.

foreach(var file in Request.Files)
{
// your logic
}

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900