Reputation: 139
I want to upload a file in flask peewee. I have a code but it generate "IOError: [Errno 2] No such file or directory: '/lookbook/uploads/1375134_766940759988081_2093520123_n.jpg'" error. Form :-
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
View.py :-
from werkzeug import secure_filename
import os
UPLOAD_FOLDER = '/lookbook/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/up', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
filename = secure_filename(file.filename)
#return filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('up',filename=filename))
return 'hello'
else:
return render_template('fileup.html');
Please guys help me to solve this problem.
Upvotes: 3
Views: 1183
Reputation: 368904
According to the error message, it seems like the directory /lookbook/uploads
does not exist.
>>> open('/tmp/no-such-directory/a-text-file.txt', 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/tmp/no-such-directory/a-text-file.txt'
>>> os.mkdir('/tmp/no-such-directory')
>>> open('/tmp/no-such-directory/a-text-file.txt', 'w')
<open file '/tmp/no-such-directory/a-text-file.txt', mode 'w' at 0x1ec6270>
Make sure the directory /tmp/no-such-directory
exists..
Upvotes: 1