M Prabodha
M Prabodha

Reputation: 70

How to post form data(uploaded image) in python bottle using Ajax?

This is my form

<form method='post' enctype='multipart/form-data' id='uploadForm' name='formn'>
    <input type='file' value='' name='newfile'>
    <input type='submit' name='sub' value='Submit'>
</form> 

this is my Ajax code

$("#uploadForm").submit(function(event) {
    event.preventDefault();
    var form_data = $("#uploadForm").serialize();
    //var form_data = new FormData($('#uploadForm')[0]);
    $.ajax({
        type        : "POST",
        url         : "/my_url",
        data        : form_data,
        contentType : false,
        processData : false,
        cache       : false,
        async       : false,
        success: function(data, status ) {
            alert('success');
        },
        error : function() {
            alert('fail');
        }
        })
    });

when use, var form_data = $("#uploadForm").serialize()=> form_data is empty string. when use, var form_data = new FormData($('#uploadForm')[0])=> form data is as following screen shot.

form_data

I want to pass the uploaded image into the service method. How can I access form data in my js file? Thank you.

Upvotes: 1

Views: 479

Answers (1)

Alex
Alex

Reputation: 2174

Here is a link describing how to use FormData object as well as how to submit a file using FormData.

Upvotes: 1

Related Questions