topless
topless

Reputation: 8221

Blobstore upload with javascript

This is the minimal declaration for the HTML in order to upload a file in Blobstore in the upload_url. What is required with this solution is required to click the Submit button in order the content to be submitted and get redirected. How can I do the post in the background with javascript or jQuery without losing the enctype?

<form id="upload_file" action="{{upload_url}}" enctype="multipart/form-data" method="post">
    <input type="file" name="file">
    <input type="submit" name="submit" value="Submit">
</form>

Upvotes: 3

Views: 1271

Answers (1)

Kevin P
Kevin P

Reputation: 1655

The jQuery Form plugin allows you to submit multipart forms in the background with Ajax.

Example:

$('#upload_file').submit(function() { 
    var options = { 
        clearForm: true        // clear all form fields after successful submit 
    }; 
    $(this).ajaxSubmit(options);
    return false; 
});

$('[name=submit]').click(function(){
    $('#upload_file').submit();        
});

Making this work silently requires that you replace your 'submit' input with a 'button' input:

<form id="upload_file" action="{{upload_url}}" enctype="multipart/form-data" method="post">
        <input type="file" name="file">
        <input type="button" name="submit" value="Submit">
</form>

Upvotes: 4

Related Questions