librelist archives

« back to archive

Problem with file uploads

Problem with file uploads

From:
Daniel Corbe
Date:
2011-04-26 @ 01:16
Hello Flask Community,

I'm having issues using flask to process uplaoded files.  I'm probably doing
something wrong; however when I go to check request.files it's always empty.
 When I do a "print request.files" from the code all I get is an empty dict:

10.0.11.116 - - [25/Apr/2011 21:01:25] "GET /upload HTTP/1.1" 200 -
ImmutableMultiDict([])

Here's the offending bits of code

HTML:

<FORM METHOD="post" ACTION="/upload">
<input type=file id=song name=song enctype="multipart/form-data">
<input type=submit>
</FORM>

Flask:
from flask import Flask, session, request, redirect, url_for,
render_template
app = Flask(__name__)

@app.route ('/upload', methods=['GET', 'POST'])
def uploadsong():
    if 'username' in session:
        pass
    else:
        return redirect(url_for('login'))

    db = conn.cursor()

    if request.method == "POST":
if 'song' in request.files:
            return "success?"
        else:
            print request.files
            return "FAIL"
    else:
        return render_template('upload.html')

Re: [flask] Problem with file uploads

From:
Jonathan Zempel
Date:
2011-04-26 @ 01:19
Daniel,

Move your 'enctype="multipart/form-data"' attribute up to your <form />
element - that should fix the problem.

Jonathan.

On Mon, Apr 25, 2011 at 6:16 PM, Daniel Corbe <dcorbe@gmail.com> wrote:

> Hello Flask Community,
>
> I'm having issues using flask to process uplaoded files.  I'm probably
> doing something wrong; however when I go to check request.files it's always
> empty.  When I do a "print request.files" from the code all I get is an
> empty dict:
>
> 10.0.11.116 - - [25/Apr/2011 21:01:25] "GET /upload HTTP/1.1" 200 -
> ImmutableMultiDict([])
>
> Here's the offending bits of code
>
> HTML:
>
> <FORM METHOD="post" ACTION="/upload">
> <input type=file id=song name=song enctype="multipart/form-data">
> <input type=submit>
> </FORM>
>
> Flask:
> from flask import Flask, session, request, redirect, url_for,
> render_template
> app = Flask(__name__)
>
> @app.route ('/upload', methods=['GET', 'POST'])
> def uploadsong():
>     if 'username' in session:
>         pass
>     else:
>         return redirect(url_for('login'))
>
>     db = conn.cursor()
>
>     if request.method == "POST":
> if 'song' in request.files:
>             return "success?"
>         else:
>             print request.files
>             return "FAIL"
>     else:
>         return render_template('upload.html')
>
>