JS blob example

HTML

<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script src="https://rawgit.com/bjornstar/blob-polyfill/master/Blob.js"></script>
<form>
    <textarea id="file_contents" rows="10" required></textarea>
    <input type="submit" value="Submit">
</form>

CSS

#file_contents{
    width: 100%;
}

JavaScript

var config = {
    api: 'http://google.be/',
    bucket: 'mediaaux',
	form_api_secret: 'ywgyzYrdLMEKZbOMK3BAnQISjQ4='
};

var form = document.forms[0];
form.addEventListener('submit', function(ev) {
    ev.preventDefault();

    upload();
});

function upload() {
    var path = '/upload/';
    var file_name =  'test.dta';
	var file_type = 'base64'; // or text/html
    
    var options = {
        bucket: config.bucket,
        expiration: Math.floor(new Date().getTime() / 1000) + 86400,
        'save-key': path + file_name
    };

    var policy = window.btoa(JSON.stringify(options));
    var signature = "";//CryptoJS.MD5(policy + '&' + config.form_api_secret);

    var text = document.getElementById('file_contents').value;
    var blob = new Blob([text], {type: file_type});

    var data = new FormData();
    data.append('policy', policy);
    data.append('signature', signature);
    data.append('file', blob, file_name);

    var request = new XMLHttpRequest();
    request.open('POST', config.api + options.bucket);
    request.onload = function() {
        console.log(request.response);
    };
    request.onerror = function() {
        console.log(request);
    }
    request.send(data);
}