我想向用户的烧瓶脚本发送用户上传的图像和随机数字以对其进行标识。我有一个简单的文件上传表单:
<form action = "http://localhost/uploaded" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" class="form-control-file"/>
<input type = "submit" class="btn btn-info" value="Upload Image"/>
</form>
然后将用户上传的图像通过Ajax发送到flask应用程序:
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false
然后在flask python脚本中,使用file = request.files['file']
检索图像,例如:
@app.route('/uploaded', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
这可行,但是尝试向ajax数据中添加一些数字(例如,自历元以来的毫秒数),如下所示:
var date = new Date();
var id = date.getTime();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: {file: new FormData($('form')[0]), id: id},
cache: false,
contentType: false,
processData: false
不适用于我(在flask应用程序中尝试通过request.files.getlist("file[]")
检索图像的操作没有任何回报)。应该怎么做?