我在javascript中有图像的base64编码字符串:
var theString64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAE4AAAAqCAYAAAAdz21RAAAJyklEQVRoQ+2ZC1QT2RnH70xmEt5BVCQ8BAWlArZa3FOyvt+7tuXIqvWxFltcV9cHiIAgLBCKgICo+KR68IUWlSpqrZZT3MUqYuWhLkYFy1N5GgKEV8gkMz03ZrazcZAQfOzazDk5Se589373/u433733PwgwXHoRQPSqZagEDOD0DAIDOAM4PQnoWc0QcfqCI+pA60Dq4nZgiMYeQucAACgAAKn5ppui78Fy+HlbF+Lju8Eq++QBOIb+/NBBAvvb39WvLULUqweu84XbqhcUNZjgmJRSQtF7LjUu/E8MeIj3Mj+Bq/v4+10dsqUHk6JvaKDq4ge2q4sd7C+y8svNNrYOIx93ymQzDiaLHmgGwVYf3RAqmiJXAWn6TpG4Hx862Q4GHLY5ckcahuHzd0YHjQYAEJqOo/7btgcZGZvE3swvEBTkZLXT5SzRiTKiF/6EUQPL4OCZs07/p+/REc75KjRmrqTyYV5WVlYvoy3aDtaDH2xLdNJ5lZIsTI0L2wEAUA7AFo7rlckYDDjUb+PWiUNHCO60NDV4Hd2fVKJxgAeLUkpVKvLh7tiQFRCAf3hCKBfHZqsAJW2ufRZ++ti+8jlLlph6uP4yUC6XPzEztwhSKYmrhSU393hOmLylq0t23YI/dBsCEEze3Z15IDkqMyAifiuGc2eRFNmiaaMMRv3myKTYmurKlOyMtBYIyD88IYz21VhdE5F54kBFQOSOBBzDl1AUJVEqVbmXTh36urq6GjD71ZdtbvbJcLFY/Ao8fcHBCUPthULesk+WPFEqFXl7YsO+AAColq8JHGc/0uGepL5h7rFDyXeDonf+A8VQT4Vcno7h3MkIgjjXVZUJSRInR45xLkFIQClJ5a2uzq70Z1VPCz0mTHpEUUCpIBTnEQQdxsXx2RQACkCSMoVCnonxeIs5CGJ24e/nxnp5fGwtcHJ42CZpnnokdUdpUPTOHG1fVU++m+Lg4uaPYRxviqSaoa/T546HrVq25kp/t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4aSJPmxS9p1vL7xgk7lZJHUIbkAb9Hf13MJHdUDSuUgkoqVztXoKZWxAUfDY9f0ll2PUtaxjUk9PT15f8nTfMvcrkvqPE1xoaOiApPPExESmdM48fjHZ0RI3/Y1oJGtt2VxXSf1dBZLOft6lrPQ62fxdSuo6w3ntWfWNtPJ/2Mi7jLgPCq8BnJ7T+V95tZKF5SCXjwAAAABJRU5ErkJggg=='
我尝试在Ruby中执行以下操作,以使用Prawn::Document#image
来创建包含对虾库中包含的图像的文档:
theImage = Base64.decode64(theString64)
image theImage, at: [215, 195]
并收到错误消息:pathname contains null byte
。
我需要解析这个问题还是有其他问题?我看不到字符串中有任何空字符。
我尝试过
.gsub(%r{\u0000}, "")
它没有帮助。
答案 0 :(得分:1)
答案 1 :(得分:0)
我已经找到了错误的原因,并在相同情况下将其包括给其他人,我在发送代码并在Ruby端进行解码之前使用javascript进行了编码,但是该图像没有缓冲区,因此要使此工作有效,您需要包括StringIO:
require 'base64'
require 'stringio'
theImage = StringIO.new(Base64.decode64(theString64.split(',', 2).last))
然后...
image theImage, at: [215, 195]