Created
June 11, 2020 15:12
-
-
Save aqzlpm11/d53314d46336746418033d90d03e4dc9 to your computer and use it in GitHub Desktop.
save base64 image from browser post request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
// js code | |
function di(page) { | |
var canName = "page_" + page; | |
var canvas = document.getElementById(canName); | |
var dataUrl = canvas.toDataURL(); | |
//console.log(dataUrl) | |
$.post('http://localhost:19290/'+page+'.png', dataUrl) | |
} | |
for (let i = 69; i <= 75; i++) { | |
di(i) | |
} | |
""" | |
from flask import Flask, request | |
from flask_cors import CORS | |
import base64 | |
def save_img(file_name, img_base64): | |
with open(file_name,'wb') as f: | |
img = base64.b64decode(img_base64) | |
f.write(img) | |
app = Flask(__name__) | |
CORS(app, supports_credentials=True) # 设置跨域 | |
@app.route('/<file_name>', methods=['POST']) | |
def a(file_name): | |
img = request.get_data().decode("utf-8") | |
img = img.replace("data:image/png;base64,", "") | |
save_img('abc/'+file_name, img) | |
return "" | |
if __name__ == '__main__': | |
app.run(port=19290, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment