简体   繁体   中英

Upload a file in Flask Peewee

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM