Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:46:22+00:00 2026-06-11T10:46:22+00:00

I’d like to build a simple HTML page that includes JavaScript to perform a

  • 0

I’d like to build a simple HTML page that includes JavaScript to perform a form POST with image data that is embedded in the HTML vs a file off disk.

I’ve looked at this post which would work with regular form data but I’m stumped on the image data.

JavaScript post request like a form submit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:46:23+00:00Added an answer on June 11, 2026 at 10:46 am

    ** UPDATE ** Feb. 2014 **

    New and improved version available as a jQuery plugin:
    https://github.com/CoeJoder/jquery.image.blob

    Usage:

    $('img').imageBlob().ajax('/upload', {
        complete: function(jqXHR, textStatus) { console.log(textStatus); } 
    });
    


    Requirements

    • the canvas element (HTML 5)
    • FormData
    • XMLHttpRequest.send(:FormData)
    • Blob constructor
    • Uint8Array
    • atob(), escape()

    Thus the browser requirements are:

    • Chrome: 20+
    • Firefox: 13+
    • Internet Explorer: 10+
    • Opera: 12.5+
    • Safari: 6+

    Note: The images must be of the same-origin as your JavaScript, or else the browser security policy will prevent calls to canvas.toDataURL() (for more details, see this SO question: Why does canvas.toDataURL() throw a security exception?). A proxy server can be used to circumvent this limitation via response header injection, as described in the answers to that post.

    Here is a jsfiddle of the below code. It should throw an error message, because it’s not submitting to a real URL (‘/some/url’). Use firebug or a similar tool to inspect the request data and verify that the image is serialized as form data (click “Run” after the page loads):

    POST data

    Example Markup

    <img id="someImage" src="../img/logo.png"/>
    

    The JavaScript

    (function() {
        // access the raw image data
        var img = document.getElementById('someImage');
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img, 0, 0);
        var dataUrl = canvas.toDataURL('image/png');
        var blob = dataUriToBlob(dataUrl);
    
        // submit as a multipart form, along with any other data
        var form = new FormData();
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/some/url', true);    // plug-in desired URL
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    alert('Success: ' + xhr.responseText);
                } else {
                    alert('Error submitting image: ' + xhr.status);
                }
            }
        };
        form.append('param1', 'value1');
        form.append('param2', 'value2');
        form.append('theFile', blob);
        xhr.send(form);
    
        function dataUriToBlob(dataURI) {
            // serialize the base64/URLEncoded data
            var byteString;
            if (dataURI.split(',')[0].indexOf('base64') >= 0) {
                byteString = atob(dataURI.split(',')[1]);
            }
            else {
                byteString = unescape(dataURI.split(',')[1]);
            }
    
            // parse the mime type
            var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
    
            // construct a Blob of the image data
            var array = [];
            for(var i = 0; i < byteString.length; i++) {
                array.push(byteString.charCodeAt(i));
            }
            return new Blob(
                [new Uint8Array(array)],
                {type: mimeString}
            );
        }
    })();
    

    References

    SO: ‘Convert DataURI to File and append to FormData

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.