在学习Flask文件上传过程中,按照视频教学中的示例代码敲了一遍,执行时却提示’PermissionError: [Errno 13] Permission denied: ‘E:\IDEA Projects\Data‘的错误,但是视频中却是可以正常操作。具体代码如下:
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
basepath = path.abspath(path.dirname(__file__))
upload_path = path.join(basepath, 'static\\uploads')
f.save(upload_path, secure_filename(f.filename))
return redirect(url_for('upload'))
return render_template('upload.html')
经过网络搜索,将代码更改为如下代码时,文件可以成功上传。
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
basepath = path.abspath(path.dirname(__file__))
upload_path = path.join(basepath, 'static', 'uploads', secure_filename(f.filename))
f.save(upload_path)
return redirect(url_for('upload'))
return render_template('upload.html')
经过代码对比,发现原来是save()方法的参数搞错了。
http://docs.jinkan.org/docs/flask/patterns/fileuploads.html
《Flask Web开发:基于Python的Web应用开发实战》
《Learning Python, 5th Edition》