Flask上传在服务器中找不到目录
Flask upload cannot find directory in server
我在本地机器代码中使用 Flask 上传时遇到问题,但是当使用 apache 将代码上传到服务器时显示错误
IOError: [Errno 2] No such file or directory:
u'app/static/avatars/KHUON.S.png'
代码:
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = 'app/static/avatars'
app.config['MAX_CONTENT_LENGTH'] = 1 * 600 * 600
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/User/Profile', methods=['GET', 'POST'])
def upload_profile():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
flash("File extension not allow.")
return redirect(url_for('upload_profile', upload='error'))
return render_template("profile.html")
好的,在 upload.py 你可以做类似
import os
absolute_path = os.path.abspath(UPLOAD_FOLDER+file_name)
os.path.abspath returns 给定相对路径的绝对路径。
我在本地机器代码中使用 Flask 上传时遇到问题,但是当使用 apache 将代码上传到服务器时显示错误
IOError: [Errno 2] No such file or directory: u'app/static/avatars/KHUON.S.png'
代码:
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = 'app/static/avatars'
app.config['MAX_CONTENT_LENGTH'] = 1 * 600 * 600
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/User/Profile', methods=['GET', 'POST'])
def upload_profile():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
flash("File extension not allow.")
return redirect(url_for('upload_profile', upload='error'))
return render_template("profile.html")
好的,在 upload.py 你可以做类似
import os
absolute_path = os.path.abspath(UPLOAD_FOLDER+file_name)
os.path.abspath returns 给定相对路径的绝对路径。