I'm trying to upload picture using the above code in Flask but getting the following exception while uploading : [Errno 2] No such file or directory: 'static/images/Shekhar.jpg'
UPLOAD_FOLDER = 'https://shekhuushashank.pythonanywhere.com/static/images/'
ALLOWED_EXTENSIONS = set(['jpg', 'png'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
@app.route('/avatar', methods = ['POST'])
@checklogin
def uploadavatar():
try:
if 'profile-pic' not in request.files:
return 'No photo uploaded'
file = request.files['profile-pic']
if file.filename == '':
return 'Invalid photo uploaded'
if file and allowed_file(file.filename):
if user.getAvatar() != 'avatar.png':
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(user.getAvatar())))
#file.filename = createUID(user.getUsername()) + '.' + str(tuple(ALLOWED_EXTENSIONS.intersection(set(file.filename.rsplit('.'))))[0])
file.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename)))
try:
with UseDatabase(app.config['dbconfig']) as cursor:
_SQL = """UPDATE `users` SET `avatar`=%s WHERE `email`=%s"""
cursor.execute(_SQL, (file.filename, user.getEmail()))
user.setAvatar(file.filename)
return 'Avatar updated'
except Exception as er:
return str(er)
else:
return 'Uploaded file is not a picture file'
except RequestEntityTooLarge as rete:
return 'File too large'
except Exception as er:
return str(er)