Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

send form data with file upload using ajax

// HTML
<form id="upload_form" enctype="multipart/form-data"></form>

// or specify exact data for FormData()
var formData = new FormData($('#upload_form')[0]);
formData.append('section', 'general');
formData.append('action', 'previewImg');
// Attach file
formData.append('image', $('input[type=file]')[0].files[0]); 
//Sending form


//Ajax request with jquery will looks like this:
$.ajax({
    url: 'Your url here',
    data: formData,
    type: 'POST',
    contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
    processData: false, // NEEDED, DON'T OMIT THIS
    // ... Other options like success and etc
});
Comment

upload form with doc type in ajax

$(".submitbtn").on("click", function(e) {

    var form = $("#Form");

    // you can't pass Jquery form it has to be javascript form object
    var formData = new FormData(form[0]);

    //if you only need to upload files then 
    //Grab the File upload control and append each file manually to FormData
    //var files = form.find("#fileupload")[0].files;

    //$.each(files, function() {
    //  var file = $(this);
    //  formData.append(file[0].name, file[0]);
    //});

    if ($(form).valid()) {
        $.ajax({
            type: "POST",
            url: $(form).prop("action"),
            //dataType: 'json', //not sure but works for me without this
            data: formData,
            contentType: false, //this is requireded please see answers above
            processData: false, //this is requireded please see answers above
            //cache: false, //not sure but works for me without this
            error   : ErrorHandler,
            success : successHandler
        });
    }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: react cheat sheet 
Javascript :: typescript class constructor overload 
Javascript :: access django template variable in javascript 
Javascript :: js check if function is a constructor 
Javascript :: urlencoded limit nodejs express 
Javascript :: lyrics api 
Javascript :: javascript urlsearchparams to object 
Javascript :: loading image in react js 
Javascript :: js pad 2 
Javascript :: multipline and single line regex pattern 
Javascript :: js host without port 
Javascript :: how to send enter event to input field jquery 
Javascript :: javascript reset span html 
Javascript :: wordpress not loading jquery 
Javascript :: difference between let and var 
Javascript :: javascript define global variable 
Javascript :: javascript next month from date 
Javascript :: tolocaledatestring format dd-mm-yyyy 
Javascript :: js generate random string of length 
Javascript :: google auth.onstatechange 
Javascript :: js custom event 
Javascript :: how to find repeated characters in a string in javascript 
Javascript :: insertadjacenthtml javascript 
Javascript :: array map destructuring 
Javascript :: how to add a class to an element in javascript 
Javascript :: javascript inject html 
Javascript :: close div when click outside angular 7 
Javascript :: convert an array to string javascript 
Javascript :: remove duplicate elements array javascript 
Javascript :: adjacent elements product javascript 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =