Я изменил свой файл JS следующим образом:
let b64_data;
function handleFileSelectTest(event){
//Change event for image file upload
img = document.getElementById('img');
file = event.target.files[0];
img.src = URL.createObjectURL(file);
img.onload = function(){
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.height = img.naturalHeight;
canvas.width = img.naturalWidth;
ctx.drawImage(img, 0, 0);
var uri = canvas.toDataURL('image/png'),
b64_data1 = uri.replace(/^data:image.+;base64,/, '');
b64_data = b64_data1;
}
}
и
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function() {
if(this.readyState === 4) {
log(this.responseText);
}
});
xhr.open("POST", "http://localhost:8088");
//console.log(b64_data);
xhr.send(b64_data);
Я также изменил свой код python как:
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', 'http://localhost:8887')
self.send_header("Access-Control-Allow-Credentials", "true")
self.end_headers()
content_len = int(self.headers.get('Content-Length'))
post_body = self.rfile.read(content_len)
print(post_body)
data = base64.b64decode(post_body)
with open("imageToSave1.png", "wb") as fh:
fh.write(base64.decodebytes(post_body))
ans="a b c"
self.wfile.write(ans.encode())