我有一个包含许多字段的表单,其中之一是文件上传。
在表单中有以下字段:
<div class="form-group">
<label class="control-label col-md-2">ID Ordine</label>
<div class="col-md-4">
<input class="form-control" type="text" name="id" size="100" maxlength="100" value="" autocomplete="off" required>
<span class="help-block"></span>
</div>
</div>
我在div中创建了此文件上传区域:
<div class="m-dropzone dropzone m-dropzone--primary" action="/file_upload_prodotto" id="m-dropzone-two" >
<div class="m-dropzone__msg dz-message needsclick">
<input type="file" name="file_ordine">
<h3 class="m-dropzone__msg-title">Trascina o clicca su questa finestra per aggiungere i file.</h3>
<span class="m-dropzone__msg-desc">Tieni premuto CTRL per selezionare piú file.</span>
</div>
在烧瓶中,我上传如下文件:
@app.route('/file_upload_prodotto', methods=['GET', 'POST'])
def file_upload_prodotto():
data = request.form
print(data)
if request.method == 'POST':
f = request.files['file']
os.makedirs(os.path.join(app.instance_path, 'file_prodotti'), exist_ok=True)
f.save(os.path.join(app.instance_path, 'file_prodotti', secure_filename(f.filename)))
print(f)
return 'file uploaded successfully'
我可以上传文件,没有任何问题。 我的真正问题是我必须将此文件链接到特定的ID,我在表单中有此ID。 我的想法是获取此ID,然后编写一个函数,使用我上传的文件名来更新该特定ID的数据库。 但我不知道如何获取ID。 你们可以帮我吗?
答案 0 :(得分:0)
因此,在您的表格中,您需要记录ID:
<div class="m-dropzone__msg dz-message needsclick">
<input type="file" name="file_ordine">
<input name="photo_id">
...
要访问烧瓶路线中的带有照片的证件,请执行以下操作:
...
photo_id = request.form['photo_id']