Skip to main content
 首页 » 编程设计

python之使用 flask 上传两个不同位置的文件

2024年10月24日11qq78292959

我尝试上传两个位于两个不同位置的文件,但在上传文件时,出现此错误“ValueError: View 函数未返回响应”。我尽了一切努力让它工作但没有运气。

html代码:

<form action="/NewCases/" method=post class="form-horizontal"> 
<h2>Add New Cases: </h2> 
<div class="input-group"> 
      <span class="input-group-addon">@</span> 
      <input type="text" id="casename" name="casename" class="form-control" 
        placeholder="Enter Case Name:" required> 
</div> 
<div class="input-group"> 
      <span class="input-group-addon">@</span> 
      <input type="text" id="adminname" name="adminname" class="form-control" 
        placeholder="Enter Case Name:" value="{{ current_user.username }}"> 
</div> 
<div class="input-group"> 
      <span class="input-group-addon">@</span> 
      <input type="file" id="imagefile" name="imagefile" class="form-control" 
        placeholder="Enter Hard Disk File:" required> 
</div> 
<div class="input-group"> 
      <span class="input-group-addon">@</span> 
      <input type="file" id="memimagefile" name="memimagefile" class="form-control" 
        placeholder="Enter Memory File:" required> 
</div> 
 
<div class="control-group"> 
    <div class="controls"> 
      <button type="submit" class="btn btn-success">Signup</button> 
    </div> 
</div> 
</form> 

flask 代码

@app.route('/NewCases/', methods=['GET', 'POST']) 
def NewCase(): 
    try: 
        if request.method == 'GET': 
            return render_template('admin.html', DICT=DICT) 
 
        if request.method == 'POST': 
            idtest = str(uuid.uuid4()) 
            idtestfilter = idtest.replace('-','') 
 
            adname= request.form['adminname'] 
            casen = request.form['casename'] 
            imagefile = request.files['imagefile'] 
            memimagefile =request.files['memimagefile'] 
 
 
 
      if imagefile > 0: 
              imagefilename = secure_filename(imagefile.filename) 
              file.save(os.path.join(UPLOAD_FOLDER , imagefilename)) 
              return redirect(url_for('index')) 
      if memimagefile > 0: 
              imagefilename = secure_filename(memimagefile.filename) 
              file.save(os.path.join(UPLOAD_FOLDER , memimagefile)) 
              return redirect(url_for('index')) 
 
      c, conn = connection() 
            c.execute("INSERT INTO cases(id,casename, adminname, imagepath, memimagepath) VALUES (%s, %s, %s, %s, %s)", 
                        (thwart(idtestfilter),thwart(adname),thwart(casen),thwart(imagefilename),thwart(memfilename))) 
            conn.commit() 
            flash("case inserted!") 
            c.close() 
            conn.close() 
 
 
 
            flash('Cases successfully added') 
            return redirect(url_for('EditCase')) 
    except Exception as e: 
        error = e 

请帮帮我!

请您参考如下方法:

首先要指出的是您的 Python 代码失败了,但是您捕获并忽略了所有 异常(嗯,几乎所有)。您的 Python 代码失败的主要原因是收到了错误的请求。如果您没有捕获并忽略所有异常,您就会知道。至少打印一条消息,然后再次引发异常。

无论如何,您正在上传文件,因此您需要将 HTML 表单的编码类型设置为 multipart/form-data,如下所示:

<form action="http://127.0.0.1:5000/" method=post enctype="multipart/form-data" class="form-horizontal"> 

这就是错误请求的原因。

我假设您已经导入了所有必需的模块和函数,例如 uuid、secure_filename 等,但是,您没有完全正确地保存文件。使用 imagefile.save() 和 memimagefile.save(),而不是 file.save()。

另外,因为两个文件可以同时上传,而且因为有数据库代码要运行,所以保存文件后不应该return。

最后,您的代码假定表单字段将始终存在于表单中。如果缺少一个,您的代码将因 KeyError 而失败。