尝试使用瓶框架读取文件
Trying to read in a file using bottle framework
我正在尝试使用 bottle 框架读取文件。
代码:
@app.route('/test/upload')
def upload():
return template('upload')
@app.route('/test/upload', method='POST')
def upload():
url = request.forms.get('url')
filename = request.files.get('filename')
name, ext = os.path.splitext(filename.raw_filename)
print(name)
....
return template('veredict', v = veredict)
app.run(host='localhost', port=8080, debug=True, reloader=True)
我的 HTML 代码中处理上传的部分:
<form action="/test/upload" method="post" enctype="multipart/form-filename">
<div class="form-group">
<input type="text" class="form-control-plaintext" id="url" placeholder="Enter URL" name="url">
<h6>Or submit a file containing URLs:</h6>
<input type="file" name="filename">
<div class = "mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
当我 运行 我的程序出现以下错误:
文件“fileup.py”,第 45 行,正在上传
名称,分机号 = os.path.splitext(filename.raw_filename)
AttributeError: 'NoneType' 对象没有属性 'raw_filename'
127.0.0.1 - - [02/Jul/2020 11:13:28] "POST /test/upload HTTP/1.1" 500 1417
我意识到我的错误与我读取文件的方式有关(变量“文件名”为空,我只是不确定如何修复它。
感谢任何帮助!
您应该将表单属性从 enctype="multipart/form-filename" 更改为 enctype="multipart/form-data"
我正在尝试使用 bottle 框架读取文件。
代码:
@app.route('/test/upload')
def upload():
return template('upload')
@app.route('/test/upload', method='POST')
def upload():
url = request.forms.get('url')
filename = request.files.get('filename')
name, ext = os.path.splitext(filename.raw_filename)
print(name)
....
return template('veredict', v = veredict)
app.run(host='localhost', port=8080, debug=True, reloader=True)
我的 HTML 代码中处理上传的部分:
<form action="/test/upload" method="post" enctype="multipart/form-filename">
<div class="form-group">
<input type="text" class="form-control-plaintext" id="url" placeholder="Enter URL" name="url">
<h6>Or submit a file containing URLs:</h6>
<input type="file" name="filename">
<div class = "mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
当我 运行 我的程序出现以下错误:
文件“fileup.py”,第 45 行,正在上传 名称,分机号 = os.path.splitext(filename.raw_filename) AttributeError: 'NoneType' 对象没有属性 'raw_filename' 127.0.0.1 - - [02/Jul/2020 11:13:28] "POST /test/upload HTTP/1.1" 500 1417
我意识到我的错误与我读取文件的方式有关(变量“文件名”为空,我只是不确定如何修复它。
感谢任何帮助!
您应该将表单属性从 enctype="multipart/form-filename" 更改为 enctype="multipart/form-data"