【问题标题】:Can not upload file/image in flaskapp无法在flaskapp中上传文件/图像
【发布时间】:2016-12-05 05:53:40
【问题描述】:

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

一切都很完美,除了当我尝试上传图片时,我总是看到错误:

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

我的flaskapp代码sn-p是

@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>

【问题讨论】:

  • 如果if file and allowed_file(file.filename) 没有通过或引发异常会怎样?...filename 显然不存在!
  • 是的,m 得到了,m 无法上传,所以文件名将不存在,但我必须做些什么才能通过。
  • 你能贴出allowed_file 的代码和相关的模板文件(你要从中上传文件)吗?
  • def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

标签: python flask flask-wtforms flask-appbuilder


【解决方案1】:

filename 的存在取决于几个条件:

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

因此,如果上述任何一种情况都没有发生,那么filename 将不会出现,因此您的错误消息也不会出现。

编辑:

在文件上传时引用flask docs

  1. 标签用 enctype=multipart/form-data 进行标记,并且 an 放置在该表单中。
  2. 应用程序从请求对象的文件字典中访问文件。
  3. 使用文件的 save() 方法将文件永久保存在文件系统的某个位置。

查看您提供的表单,您似乎没有任何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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2022-07-29
    • 2017-10-15
    相关资源
    最近更新 更多