Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

mvc, ajax upload with data

https://stackoverflow.com/questions/32470906/how-to-upload-a-image-in-asp-net-mvc-using-ajax-request

I use above codes to upload a file. but if I want to add a parameter from <input type="text" id="firstname"> to ajax like below. How can I do that? Basically, I upload the image but I also want to update the database behind.


type: "POST",
           url: '/Home/Upload',
           data: formData, document.getelementid("firstname").value,
           dataType: 'json',
           contentType: false,
Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

any helps
Avatar of hielo
append the field to the formData object before the ajax call:
...
         for (var i = 0; i < totalFiles; i++) {
           var file = document.getElementById("imageUploadForm").files[i];
           formData.append("imageUploadForm", file);
         }

         formData.append("firstname", $("#firstname").val() );

         $.ajax({
           type: "POST",
           url: '/Home/Upload',
           data: formData,
           dataType: 'json',
           contentType: false,
           processData: false
         }).done(function(data) {
           alert('success');
           console.log(data);
         }).fail(function( xhr, status, errorThrown ) {
             alert('fail');
         });

Open in new window

thanks but how to call each parameter in the mvc controller?
I am not a .Net developer, however, I did some searching and here's what I found.  You can retrieve the files using:
HttpPostedFileBase file = Request.Files[i];

Open in new window

see http://ajeeshms.in/articles/upload-files-using-ajax-in-asp-net-mvc/

As for the non-file data, try retrieving the values from Request.Form:
Request.Form("firstname")

Open in new window

that won't work to me because it accepts more than 1 files from the upload.
how to append upload file just like the following?

formData.append("firstname", $("#firstname").val() );
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how to call my file on mvc controller?