linux - 如何在 linux bash/shell 中对图像进行 base64 编码

我正在尝试在 shell 脚本中对图像进行 base64 编码并将其放入变量中:

test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH

我也试过这样的:

test=\`echo -ne DSC_0251.JPG | base64\`

但仍然没有成功。

我想做这样的事情:

curl -v -X POST -d '{"image":$IMAGE_BASE64,"location":$LOCATION,"time_created":$TIMECREATED}' -H 'Content-type: text/plain; charset=UTF8' http://192.168.1.1/upload

我找到了 http://www.zzzxo.com/q/answers-bash-base64-encode-script-not-encoding-right-12290484.html

但仍然没有成功。

最佳答案

您需要使用 cat 来获取名为“DSC_0251.JPG”的文件的内容,而不是文件名本身。

test="$(cat DSC_0251.JPG | base64)"

但是,base64 可以从文件本身读取:

test=$( base64 DSC_0251.JPG )

https://stackoverflow.com/questions/16918602/

相关文章:

linux - 如何在 Ubuntu 中增加 Neo4j 的最大文件打开限制(ulimit)?

python - 重命名 Pandas 中的特定列

linux - GNU screen 没有响应,似乎被阻塞了

python - 如何检查两个日期之间的差异(以秒为单位)?

python - Python 有包/模块管理系统吗?

python - Django 可选 url 参数

python - 如何将一串字节转换为int?

linux - 解压 tar.gz 文件

python - 将一对列表/元组解压缩成两个列表/元组

linux - 如何强制 'cp' 覆盖目录而不是在里面创建另一个目录?