Hi everyone,
I'm trying to get Flask-Uploads working, but get the following error
when I try to upload a file:
TypeError: storage must be a werkzeug.FileStorage
My views.py contains the following:
from flask import flash, redirect, render_template, request
from flaskext.uploads import UploadSet, configure_uploads, IMAGES,
UploadNotAllowed
from nepenthe import app
from nepenthe.database import db_session
UPLOADED_PHOTOS_DEST = '/tmp/photos'
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
@app.route('/admin/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
photo = request.files['photo']
title = request.form['title']
caption = request.form['caption']
try:
filename = photos.save('photo') ## This line
except UploadNotAllowed:
flash(u'The upload was not allowed')
else:
file = File(title, filename, caption)
db_session.add(entry)
db_session.commit()
flash(u'Photo saved')
return redirect(url_for('index'))
return render_template('upload.html')
And upload.html is as follows:
{% extends "layout.html" %}
{% block title %}Upload file{% endblock %}
{% block body %}
<form action="{{ url_for('upload_file') }}" method=POST
enctype=multipart/form-data>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>File:
<dd><input type=file name=photo>
<dt>Caption:
<dd><textarea name=caption rows=5 cols=40></textarea>
</dl>
<p><input type=submit value=Save>
</form>
{% endblock %}
The error message refers to the line "filename = photos.save('photo')".
I haven't found many examples of Flask-Upload in use. Is anyone here
using it? Could anyone suggest how to fix this error?
Richard
Shouldn't it be photos.save(request.files['photo']) ? It's expecting a FileStorage instance, you're passing a string. On 16 November 2010 01:59, Richard Austin (오현성) <chamdarae@gmail.com> wrote: > Hi everyone, > > I'm trying to get Flask-Uploads working, but get the following error > when I try to upload a file: > > TypeError: storage must be a werkzeug.FileStorage > > My views.py contains the following: > > from flask import flash, redirect, render_template, request > from flaskext.uploads import UploadSet, configure_uploads, IMAGES, > UploadNotAllowed > from nepenthe import app > from nepenthe.database import db_session > > UPLOADED_PHOTOS_DEST = '/tmp/photos' > > photos = UploadSet('photos', IMAGES) > configure_uploads(app, photos) > > @app.route('/admin/upload', methods=['GET', 'POST']) > def upload_file(): > if request.method == 'POST': > photo = request.files['photo'] > title = request.form['title'] > caption = request.form['caption'] > try: > filename = photos.save('photo') ## This line > except UploadNotAllowed: > flash(u'The upload was not allowed') > else: > file = File(title, filename, caption) > db_session.add(entry) > db_session.commit() > flash(u'Photo saved') > return redirect(url_for('index')) > return render_template('upload.html') > > And upload.html is as follows: > > {% extends "layout.html" %} > {% block title %}Upload file{% endblock %} > {% block body %} > <form action="{{ url_for('upload_file') }}" method=POST > enctype=multipart/form-data> > <dl> > <dt>Title: > <dd><input type=text size=30 name=title> > <dt>File: > <dd><input type=file name=photo> > <dt>Caption: > <dd><textarea name=caption rows=5 cols=40></textarea> > </dl> > <p><input type=submit value=Save> > </form> > {% endblock %} > > The error message refers to the line "filename = photos.save('photo')". > > I haven't found many examples of Flask-Upload in use. Is anyone here > using it? Could anyone suggest how to fix this error? > > Richard >
Thanks, Danjac354! Yes, it should... Fixed. Richard 2010/11/16 danjac354@gmail.com <danjac354@gmail.com>: > Shouldn't it be photos.save(request.files['photo']) ? It's expecting a > FileStorage instance, you're passing a string. > > On 16 November 2010 01:59, Richard Austin (오현성) <chamdarae@gmail.com> wrote: >> Hi everyone, >> >> I'm trying to get Flask-Uploads working, but get the following error >> when I try to upload a file: >> >> TypeError: storage must be a werkzeug.FileStorage >> >> My views.py contains the following: >> >> from flask import flash, redirect, render_template, request >> from flaskext.uploads import UploadSet, configure_uploads, IMAGES, >> UploadNotAllowed >> from nepenthe import app >> from nepenthe.database import db_session >> >> UPLOADED_PHOTOS_DEST = '/tmp/photos' >> >> photos = UploadSet('photos', IMAGES) >> configure_uploads(app, photos) >> >> @app.route('/admin/upload', methods=['GET', 'POST']) >> def upload_file(): >> if request.method == 'POST': >> photo = request.files['photo'] >> title = request.form['title'] >> caption = request.form['caption'] >> try: >> filename = photos.save('photo') ## This line >> except UploadNotAllowed: >> flash(u'The upload was not allowed') >> else: >> file = File(title, filename, caption) >> db_session.add(entry) >> db_session.commit() >> flash(u'Photo saved') >> return redirect(url_for('index')) >> return render_template('upload.html') >> >> And upload.html is as follows: >> >> {% extends "layout.html" %} >> {% block title %}Upload file{% endblock %} >> {% block body %} >> <form action="{{ url_for('upload_file') }}" method=POST >> enctype=multipart/form-data> >> <dl> >> <dt>Title: >> <dd><input type=text size=30 name=title> >> <dt>File: >> <dd><input type=file name=photo> >> <dt>Caption: >> <dd><textarea name=caption rows=5 cols=40></textarea> >> </dl> >> <p><input type=submit value=Save> >> </form> >> {% endblock %} >> >> The error message refers to the line "filename = photos.save('photo')". >> >> I haven't found many examples of Flask-Upload in use. Is anyone here >> using it? Could anyone suggest how to fix this error? >> >> Richard >> >