【发布时间】:2016-08-02 01:53:18
【问题描述】:
在遵循有关如何上传文件的 Flask 文档/教程时,我使用了以下代码:
main.py:
try:
# import python modules
import sys
import config
import os
import inspect
import datetime
from flask import Flask,render_template,jsonify,redirect,url_for,request
from werkzeug import secure_filename
except ImportError as e:
print "Import error:", e, "\nAborting the program", __version__
sys.exit()
app = Flask(__name__)
app.config.from_object('config.BaseConfig')
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route("/upload", methods=['POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = os.path.join(app.config['CSV_FOLDER'], filename)
return jsonify({"success": "True", "post": "none"})
else:
return jsonify({"success": "False", "post": "Invalid filename"})
else:
return jsonify({"success": "False", "post": "POST request required!"})
config.py:
class BaseConfig(object):
DEBUG = True
TESTING = False
PROPAGATE_EXCEPTIONS = True
SECRET_KEY = ""
CSV_FOLDER = "static/csv"
当在我的 Windows 机器上的 localhost 上运行时,上面的效果很好。当我将它上传到我的 Hostmonster 网站时,我得到一个 404 http-status-code(找不到页面)。
这是我的 Hostmonster .htaccess 文件(存储在 /GKS 中):
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteBase /GKS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ gks.fcgi/$1 [QSA,L]
这是我在 Hostmonster 网站上的文件夹结构的图片: Hostmonster folder structure 如果我查看浏览器开发控制台,我会看到这个附加错误消息:“主线程上的同步 XMLHttpRequest 已被弃用,因为它对最终用户的体验产生不利影响。有关更多帮助,请查看 https://xhr.spec.whatwg.org/。”。不确定这是否与我的文件上传有关。
请帮忙,因为我正在努力解决这个问题。
【问题讨论】:
标签: python file flask upload http-status-code-404