我正在尝试在 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/