无法在 flaskapp 中上传 file/image

Can not upload file/image in flaskapp

我在 html/php 中创建了一个表单,并使用 flask 作为后端。

一切正常,除了我尝试上传图片时,我总是看到错误:

error is**"UnboundLocalError: local variable 'filename' referenced before assignment"**

我的 flaskapp 代码片段是

@app.route('/apple', methods=['GET', 'POST'])
def apple():
  onlineAppForm = RegForm()
  if request.method == 'POST':
    try:
        file = request.files['file']
        if file and allowed_file(file.filename):
           filename = secure_filename(file.filename)
           file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    except Exception as e:
        print "Form without file "+str(e)
    return render_template("apply2.html", data=data, filename=filename, form=onlineAppForm)

这是我的上传文件夹

UPLOAD_FOLDER = 'static/uploads'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = os.urandom(24)

我不知道错误在哪里。

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

这是我的表格

<tr>
    <td class="sectionheading">Upload Scanned Copies</td>
</tr>
<tr>
    <td height="12">&nbsp;</td>
</tr>
<tr id="photosignthumb_span" height="40">
    <td align="left" valign="middle" width="100%">&nbsp;<b>
        <span class="mandatory">*</span>
        Please upload scanned copies of your photo </b>
        <input type="file" name="photo" id="photofile"  
         method="post"/><br>
        Please upload your recent passport size photograph:max 
        80KB(Only JPEG and JPG formats)
    </td>
</tr>
<tr id="photosignthumb_span" height="40">
    <td align="left" valign="middle" width="100%">&nbsp;<b>
        <span class="mandatory">*</span>
        Please upload scanned copies of your signature </b> 
        <input type="file" name="sign" id="signfile" 
        method="post"/><br>
        Please upload your recent passport size photograph:max 
        80KB(Only JPEG and JPG formats)
    </td>
</tr>

filename 存在取决于几个条件:

1 - file = request.files['file']
2 - if file and allowed_file(file.filename):
3 - 未引发 异常

因此,如果以上任何一项都没有发生,那么 filename 将不会 spring 存在,因此不会出现您的错误消息。

编辑:

在文件上传时引用 flask docs

  1. A tag is marked with enctype=multipart/form-data and an is placed in that form.
  2. The application accesses the file from the files dictionary on the request object.
  3. Use the save() method of the file to save the file permanently somewhere on the filesystem.

查看您提供的表格,您似乎没有任何 form 块,您应该有如下内容:

<form action="/apple" method="post" enctype="multipart/form-data">
<tr>
    <td class="sectionheading">Upload Scanned Copies</td>
</tr>
<tr>
    <td height="12">&nbsp;</td>
</tr>
<tr id="photosignthumb_span" height="40">
    <td align="left" valign="middle" width="100%">&nbsp;<b>
        <span class="mandatory">*</span>
        Please upload scanned copies of your photo </b>
        <input type="file" name="photo" id="photofile"  
         method="post"/><br>
        Please upload your recent passport size photograph:max 
        80KB(Only JPEG and JPG formats)
    </td>
</tr>
<tr id="photosignthumb_span" height="40">
    <td align="left" valign="middle" width="100%">&nbsp;<b>
        <span class="mandatory">*</span>
        Please upload scanned copies of your signature </b> 
        <input type="file" name="sign" id="signfile" 
        method="post"/><br>
        Please upload your recent passport size photograph:max 
        80KB(Only JPEG and JPG formats)
    </td>
</tr>
</form>