Search code examples
javascriptbase64data-uridata-url

How to create a base64 file from nothing?


I want to be able to create base64 files (images, sounds, video) without any previous models. For example, if I want to create a base64 64px*64px red image, how can I do this without creating first a canvas?

I would also like to create a sound (note) with no model.

I've searched on Google for some documentation on base64 encoding but I did not seem to find specific things for my need.

I am going to use Javascript, but I guess this should be the same for every language.


Solution

  • Try

    function createFile(_data) {
      var _data = ["<!doctype html>", 
            "<img style=width:64px;height:64px;"
            + "background-color:red;display:block; />"];
      var data = window.btoa(_data.join("").toString());
      var file = "data:text/html;base64," + data;
      return file
    };
    createFile();
    

    jsfiddle http://jsfiddle.net/guest271314/6GPju/

    see also http://www.w3.org/TR/FileAPI/ , https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa