简体   繁体   English

使用Python Flask从Cropit接收HTML发布

[英]Receiving HTML post from cropit with Python Flask

I am new to web development and have a small web app where I need a user to upload a cropped image. 我是Web开发的新手,并且有一个小型Web应用程序,需要用户上传裁剪后的图像。 My issue is that I cannot fetch the image that is send in the jQuery post request. 我的问题是我无法获取jQuery post请求中发送的图像。

My Javascript and HTML looks like this: 我的Javascript和HTML如下所示:

 function sendImage(imageData) { var info = {} info.upload = imageData info.gender = gender.value info.ageGroup = ageGroup.value $.ajax({ type: "POST", url: "/uploadToS3", data: info, enctype: 'multipart/form-data', dataType: "json", success: console.log("image written") }); } 
  <div class="image-editor"> <input type="file" class="cropit-image-input"> <div class="cropit-preview"></div> <div class="image-size-label"> Resize image </div> <input type="range" class="cropit-image-zoom-input"> <button class="export">Export</button> </div> 

Here is the python code handling the post request 这是处理发布请求的python代码

@app.route('/uploadToS3', methods = ['POST'])
def uploadToS3():
    username = session.get('username')
    image = request.files.get('upload')
    gender = request.form['gender']
    ageGroup = request.form['ageGroup']
    fileName = getNewFileName()
    print(image)
    return redirect('/')

image prints as 'None', the issue seems to be in the line where I request the upload parameter. 图片打印为“无”,问题似乎出在我请求上载参数的行中。 The data being posted to Flask (taken from the form data) is upload:data:image/png;base64 and a very long string. 要发布到Flask的数据(从表单数据中获取)是upload:data:image / png; base64和一个很长的字符串。 So it looks like it posting correctly. 因此,它看起来像正确发布。

Any hints would be greatly appreciated 任何提示将不胜感激

According to the answers to this question you can't just use an simple object as data when passing files. 根据该问题的答案,您不能仅在传递文件时将简单的对象用作data

For that purpose you have to do the following: 为此,您必须执行以下操作:

var formData = new FormData();
formData.append("gender",gender.value); // And so on...
formData.append("upload",$("#file")[0].files[0]); // When your file input has id="file"

$.ajax({
  type: "POST",
  url: "/uploadToS3",
  data: formData,
  enctype: 'multipart/form-data',
  // And so on...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM