2 回答
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:)
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()}`")
添加回答
举报