亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

由于“源丟失”而無法打開 base64 解碼的“.jpg”圖像?

由于“源丟失”而無法打開 base64 解碼的“.jpg”圖像?

斯蒂芬大帝 2021-12-21 16:04:58
我正在嘗試.jpg使用requestdjango 服務器端發送文件并嘗試對其進行解碼。代碼:這是發送方代碼:import requestsimport osimport base64fPath = os.getcwd()url = 'http://127.0.0.1:8000/submitcausedata/'headers = {'content-type': 'application/x-www-form-urlencoded'}path_img = fPath + '/image13.jpg'data = open(path_img,'rb').read()encoded_image = base64.encodestring(data)   print(encoded_image[:10])r = requests.post(url,data=encoded_image,headers=headers)   在接收端代碼:@csrf_exemptdef submitCauseData(request):       response_data = {}      if request.method == 'POST':        data = request.POST             myDict = dict(data)             imageStr = list(myDict.keys())              imageStr = imageStr[0]        print(imageStr[:10])        image_result = open('image.jpg', 'wb')               image_result.write(base64.b64decode(imageStr))        return HttpResponse("Page Exists")      所以,代碼正在執行,但是當我嘗試打開保存的圖像時,它顯示錯誤Photo Source File missing?發送代碼輸出:print(encoded_image[:10])----> b'/9j/4WQYRX'接收端代碼輸出:print(imageStr[:10])----> /9j/4WQYRX更新:.jpg使用.txt轉換比較兩個文件時,使用DiffCheckerOnline比較它們中的許多值是不同的。Ubuntu 上的圖像查看器在打開.jpg接收端時顯示錯誤:Error interpreting JPEG image file (Unsupported marker type 0x10)還:在發送端:print(len(data))print(type(data))print(len(encoded_image))print(type(encoded_image))OUTPUT:171062<class 'bytes'>228084<class 'bytes'>在接收端:print(len(imageStr))print(type(imageStr))print(len(imagedec))print(type(imagedec))OUTPUT:228083<class 'str'>168972<class 'bytes'>
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

我發現了錯誤,當我嘗試發送的class的byte從發送方的字符串,會發生什么情況是所有的'+',并'='得到轉化為' '接收側。


因此,通過使用:


發送方:


import requests

import os

import base64

fPath = os.getcwd()

url = 'http://127.0.0.1:8000/submitcausedata/'

headers = {'content-type': 'application/x-www-form-urlencoded'}

path_img = fPath + '/image13.jpg'

data = open(path_img,'rb').read()

encoded_image = base64.b64encode(data)  

r = requests.post(url,data=encoded_image,headers=headers)       

接收方:


@csrf_exempt

def submitCauseData(request):   

    response_data = {}      

    if request.method == 'POST':

        data = request.POST     

        myDict = dict(data)     

        imageStr = list(myDict.keys())      

        image = imageStr[0].replace(' ','+')

        image = image.encode() + b'==='                             

        imagedec = base64.b64decode(image)

        fPath = os.getcwd()     

        image_result = open(fPath + '/image.jpg', 'wb')

        image_result.write(imagedec)

        image_result.close()            

        return HttpResponse("HELLO")        

我解決了錯誤。


無論如何,感謝您的幫助@waket-zheng:)


查看完整回答
反對 回復 2021-12-21
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

下面的代碼有效嗎?


發送


from base64 import b64encode

from pathlib import Path


import requests



url = "http://127.0.0.1:8000/submitcausedata/"

headers = {"content-type": "application/x-www-form-urlencoded"}

encoded_image = b64encode(Path("image13.jpg").read_bytes())

print(encoded_image[:10])

r = requests.post(url, data=encoded_image, headers=headers)

接收


from base64 import b64decode

from pathlib import Path    


@csrf_exempt

def submitCauseData(request):

    response_data = {}

    if request.method == "POST":

        imageStr = list(dict(request.POST).keys())[0]

        print(imageStr[:10])

        p = Path("image.jpg")

        p.write_bytes(b64decode(imageStr))

        return HttpResponse(f"Saved image to `{p.resolve()}`")


查看完整回答
反對 回復 2021-12-21
  • 2 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號