Search code examples
pythonhtmlframeworksbottle

Trying to read in a file using bottle framework


I'm trying to read in a file using the bottle framework.

Code:

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

And the part of my HTML code that handles the upload:

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

When i run my program I get the following error:

File "fileup.py", line 45, in upload name, ext = os.path.splitext(filename.raw_filename) AttributeError: 'NoneType' object has no attribute 'raw_filename' 127.0.0.1 - - [02/Jul/2020 11:13:28] "POST /test/upload HTTP/1.1" 500 1417

I realize my error has something to do with how I'm reading in the file (the variable "filename" is empty, I'm just not sure how to go about fixing it.

Any help is appreciated!


Solution

  • You should change the form attribute from enctype="multipart/form-filename" to enctype="multipart/form-data"