HTML img 태그에 직접 데이터를 넣는 방법은 아래와 같다
바이너리 데이터를 base64로 인코딩하여 넣는다.
이미지 타입이 gif이면 data:image/gif, png이면 data:image/png등으로 설정한다
<img src=”data:image/gif;base64,R0lGODlhagJlAfcAA…..” alt=”Embedded Image” />
파일인 경우
<a href=”data:file/zip;base64,UEsDBBQAAAAIABakTke…” download=”a.zip”>zip</a>
==============base64 encoding =======================
import base64
# Load this source file and strip the header.
fr = open('c:\me\excel.jpg', 'rb')
fw = open('a.txt','wb')
data = fr.read()
encoded_data = base64.b64encode(data)
fw.write(encoded_data)
while data:
data = fr.read()
encoded_data = base64.b64encode(data)
fw.write(encoded_data)
fr.close()
fw.close()
==============base64 decoding =======================
import base64
# Load this source file and strip the header.
fr = open('aa.ppy', 'rb')
fw = open('aa.xps','wb')
data = fr.read()
decoded_data = base64.b64decode(data)
fw.write(decoded_data)
while data !="":
data = fr.read()
decoded_data = base64.b64decode(data)
fw.write(decoded_data)
fr.close()
fw.close()
[출처] http://www.complexity.co.kr/?p=2171

