第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用Javascript檢索二進(jìn)制文件內(nèi)容,對(duì)base64進(jìn)行編碼,然后使用Python對(duì)其進(jìn)行反解

使用Javascript檢索二進(jìn)制文件內(nèi)容,對(duì)base64進(jìn)行編碼,然后使用Python對(duì)其進(jìn)行反解

慕無(wú)忌1623718 2019-11-11 14:24:37
我正在嘗試使用XMLHttpRequest(使用最新的Webkit)下載二進(jìn)制文件,并使用此簡(jiǎn)單功能對(duì)base64的內(nèi)容進(jìn)行編碼:function getBinary(file){    var xhr = new XMLHttpRequest();      xhr.open("GET", file, false);      xhr.overrideMimeType("text/plain; charset=x-user-defined");      xhr.send(null);    return xhr.responseText;}function base64encode(binary) {    return btoa(unescape(encodeURIComponent(binary)));}var binary = getBinary('http://some.tld/sample.pdf');var base64encoded = base64encode(binary);附帶說(shuō)明一下,以上所有內(nèi)容都是標(biāo)準(zhǔn)Javascript內(nèi)容,包括btoa()和encodeURIComponent():https : //developer.mozilla.org/en/DOM/window.btoa這工作非常順利,我什至可以使用Javascript解碼base64內(nèi)容:function base64decode(base64) {    return decodeURIComponent(escape(atob(base64)));}var decodedBinary = base64decode(base64encoded);decodedBinary === binary // true現(xiàn)在,我想使用Python解碼base64編碼的內(nèi)容,該內(nèi)容使用一些JSON字符串來(lái)獲取base64encoded字符串值。天真的,這就是我的工作:import urllibimport base64# ... retrieving of base64 encoded string through JSONbase64 = "77+9UE5HDQ……………oaCgA="source_contents = urllib.unquote(base64.b64decode(base64))destination_file = open(destination, 'wb')destination_file.write(source_contents)destination_file.close()但是生成的文件無(wú)效,看起來(lái)該操作已被UTF-8,編碼或其他尚不清楚的東西弄亂了。如果在將UTF-8內(nèi)容放入目標(biāo)文件之前嘗試對(duì)其進(jìn)行解碼,則會(huì)引發(fā)錯(cuò)誤:import urllibimport base64# ... retrieving of base64 encoded string through JSONbase64 = "77+9UE5HDQ……………oaCgA="source_contents = urllib.unquote(base64.b64decode(base64)).decode('utf-8')destination_file = open(destination, 'wb')destination_file.write(source_contents)destination_file.close()$ python test.py// ...UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 0: ordinal not in range(128)附帶說(shuō)明一下,這是同一文件的兩種文本表示形式的屏幕截圖;左:原件;右:從base64解碼的字符串創(chuàng)建的一個(gè):http://cl.ly/0U3G34110z3c132O2e2x嘗試重新創(chuàng)建文件時(shí),是否存在已知的技巧來(lái)規(guī)避編碼方面的這些問(wèn)題?您將如何實(shí)現(xiàn)自己?任何幫助或暗示非常感謝:)
查看完整描述

2 回答

  • 2 回答
  • 0 關(guān)注
  • 637 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)