How to create a base64 file from nothing?

戏子无情 提交于 2019-12-12 02:38:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/24227331/how-to-create-a-base64-file-from-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!