业余编码员和发烧友在这里,第一次问...我正在研究CS50课程的最终项目。我正在建立一个网站,您可以在其中注册和上传某些文档。一切似乎都运行良好,但是当我尝试上传文档时,出现内部服务器错误,并显示以下消息:
服务器遇到内部错误,无法完成您的请求。服务器超载或应用程序中有错误。
这是怎么回事?在CS50 IDE中将文档上载到文件夹是否有限制?我似乎找不到解决此问题的方法。文件必须为PDF格式,我已经尝试过/ documentos,/ fp / documentos和〜/ workspace / fp / documentos /作为文件路径。这是代码:
import os
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session,
url_for
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions
from werkzeug.security import check_password_hash, generate_password_hash
from werkzeug.utils import secure_filename
from helpersp import apology, login_required
# Define uploaded documents destination and allowed extensions
UPLOAD_FOLDER = '/fp/documentos/'
ALLOWED_EXTENSIONS = set(['pdf'])
# Check if extension is allowed.
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/primer", methods=["GET", "POST"])
@login_required
def primer():
"""Prompts user to upload pre contractual documents"""
if request.method == "GET":
return render_template("primer.html")
else:
# check if the post request has the file part
if "file" not in request.files:
return render_template("sindocumentos.html")
file = request.files["file"]
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == "":
return render_template("sindocumentos.html")
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['/fp/documentos/'], filename))
return render_template("successp.html")
这是HTML部分:
{% extends "layoutp.html" %}
{% block title %}
Subir carpeta
{% endblock %}
{% block main %}
<form action="/primer" method="post" enctype=multipart/form-data>
<div>
Sube tu carpeta de documentos en un solo archivo PDF.
</div>
<div>
El nombre del archivo debe tener el siguiente formato:
carpeta_nombre_apellido_año.pdf.
</div>
<div class="form-group">
<input type="file" name="file">
</div>
<button class="btn btn-primary" type="submit">Subir</button>
</form>
{% endblock %}
烧瓶跟踪如下:
ERROR:flask.app:Exception on /primer [POST]
Traceback (most recent call last):
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/workspace/fp/helpersp.py", line 34, in decorated_function
return f(*args, **kwargs)
File "/home/ubuntu/workspace/fp/profesores.py", line 133, in primer
file.save(os.path.join(app.config[UPLOAD_FOLDER], filename))
KeyError: '/documentos'
更新:我尝试了不同的绝对路径,但没有任何运气。我试过使用os.path.abspath(os.path.dirname( file ))和UPLOAD_FOLDER = os.mkdir('documentos2')。使用os.mkdir选项,它会给我与以前相同的错误消息,只是此类型的KeyError为'None':
File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/workspace/fp/helpersp.py", line 34, in decorated_function
return f(*args, **kwargs)
File "/home/ubuntu/workspace/fp/profesores.py", line 124, in primer
file.save(os.path.join(app.config[UPLOAD_FOLDER], filename))
KeyError: None
非常感谢您的帮助!
答案 0 :(得分:0)
尝试这样的事情:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
代替
file.save(os.path.join(app.config['/fp/documentos/'], filename))
不确定是否可以解决您的问题,但这就是我的工作文件的外观,就像我使用与您使用的相同属性一样。这是我唯一能找到的不同之处。它也可能是路径,您确定在代码中定义了完整路径,并且在执行发布请求时是否看到在该路径中上传的任何文件?
完整路径示例:
/Users/username/Desktop/csv_tool/upload_folder