Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Last active September 24, 2017 01:34
Show Gist options
  • Save YujiSODE/009cdfb080e273ec506a1ef24acd050c to your computer and use it in GitHub Desktop.
Save YujiSODE/009cdfb080e273ec506a1ef24acd050c to your computer and use it in GitHub Desktop.
it returns a data url with base64-encoded data.
#dataurl64
#dataurl64.tcl
##===================================================================
# Copyright (c) 2017 Yuji SODE <yuji.sode@gmail.com>
#
# This software is released under the MIT License.
##===================================================================
#it returns a data url with base64-encoded data.
#it requires Tcl 8.6+.
#=== Synopsis ===
#dataurl64 fileName ?type?;
#=== Parameters ===
# - fileName: file path
# - type: MIME type; text/plain as default value
##===================================================================
proc dataurl64 {fileName {type text/plain}} {
set c [open $fileName r];
#read the channel as binary
fconfigure $c -encoding binary -translation binary;
set v [read $c];close $c;
#data URL scheme is returned as following form:
#data:<mediatype>;base64,<base64-encoded data>
return "data:$type;base64,[binary encode base64 $v]";
};
#*** References ***
# - Masinter, L. 1998. RFC 2397 - The "data" URL scheme. Requests for Comments. Internet Engineering Task Force. derived on [2017-09-13] from: https://tools.ietf.org/html/rfc2397
# - Mozilla and individual contributors. 2005-2017. Data URLs - HTTP | MDN. MDN Web Docs. derived on [2017-09-13] from: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
#*** License ***
#MIT License
#
#Copyright (c) 2017 Yuji Sode
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment