How to Get a Base64 Version of a File From Command Line

A while back I wrote an article on how to Convert Image to Data URI with JavaScript. It’s a neat trick developers can use for any number of reasons. Instead of abusing canvas, however, why not simply get the base64 data from command line? You can use base64 and pbcopy to convert a file to […]

The post How to Get a Base64 Version of a File From Command Line appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

A while back I wrote an article on how to Convert Image to Data URI with JavaScript. It’s a neat trick developers can use for any number of reasons. Instead of abusing canvas, however, why not simply get the base64 data from command line?

You can use base64 and pbcopy to convert a file to base64 and copy it to the clipboard:

# base64 gets data, pbcopy copies to clipboard
base64 -i logo.jpeg | pbcopy

Once you have the file data copied in base64 format, the URL format to use the data is:

# data:{mime-type};base64,{data}
data:image/jpeg;base64,/9j/4AAQSkZJRgAB......

While base64 data and data URIs do look cryptic, they’re useful to avoid making requests to other files. I use them when creating presentations or when I can’t count on a decent internet connection.

The post How to Get a Base64 Version of a File From Command Line appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2023-04-13T20:21:27+00:00) How to Get a Base64 Version of a File From Command Line. Retrieved from https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/

MLA
" » How to Get a Base64 Version of a File From Command Line." David Walsh | Sciencx - Thursday April 13, 2023, https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/
HARVARD
David Walsh | Sciencx Thursday April 13, 2023 » How to Get a Base64 Version of a File From Command Line., viewed ,<https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/>
VANCOUVER
David Walsh | Sciencx - » How to Get a Base64 Version of a File From Command Line. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/
CHICAGO
" » How to Get a Base64 Version of a File From Command Line." David Walsh | Sciencx - Accessed . https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/
IEEE
" » How to Get a Base64 Version of a File From Command Line." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/. [Accessed: ]
rf:citation
» How to Get a Base64 Version of a File From Command Line | David Walsh | Sciencx | https://www.scien.cx/2023/04/13/how-to-get-a-base64-version-of-a-file-from-command-line/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.