采用base64编码前后端传递图片,但是后端始终没法保存,不知道哪里出了问题,前端的数据应该没问题啊
function upload() {
let formData = new FormData($("#form1")[0])
console.log('点击提交之后,打印FormData中的数据')
console.log(formData.get('file'))
$.ajax({
url: 'http://127.0.0.1:5000/upload',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function(returndata) {
alert(returndata);
},
error: function(error) {
alert(error);
}
})
}
上面是前端
@app.route('/upload', methods=['POST'])
def upload():
# upload_file = request.files.get('file')
# print(upload_file)
other = request.form["name"]
print(other)
mydict = {"id": request.form["id"], "name": request.form["name"], "balance": request.form["balance"],
"photo": request.form["show"]}
base647 = request.form["show"]
# 如何把代码保存为png格式
#imagedata = base64.b64decode(base647)
# io.imshow(imagedata)
# io.imsave('static/photo/1.jpg',imagedata)
c = re.sub(r'%0A', "\\n", f)
d = re.sub(r'data:image/jpg;base64,', "", c)
imgdata = base64.b64decode(d)
file = open('static/photo/3.jpg', 'wb')
file.write(imgdata)
file.close()
return render_template('test.html',a = base647)
谢谢大佬们
不知道你这个问题是否已经解决, 如果还没有解决的话: