-1
我想用Flask上传文件。 这里是我的HTML代码烧瓶:没有符合给定的URI
<!DOCTYPE html>
<html>
<head>
<title>Python Starter Application</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="stylesheets/style.css" />
</head>
<body>
<form action="/upload">
File input
<input type="file" name="InputFile">
<button type="submit" class="btn btn-default">Upload</button>
</form>
</body>
</html>
这里是我的烧瓶代码
import os
from flask import Flask, request
import swiftclient
try:
from SimpleHTTPServer import SimpleHTTPRequestHandler as Handler
from SocketServer import TCPServer as Server
except ImportError:
from http.server import SimpleHTTPRequestHandler as Handler
from http.server import HTTPServer as Server
app = Flask(__name__)
# Read port selected by the cloud for our application
PORT = int(os.getenv('PORT', 8000))
# Change current directory to avoid exposure of control files
os.chdir('static')
httpd = Server(("", PORT), Handler)
# connections
auth_url = ##
project = ##
projectId = ##
region = ##
userId = ##
username = ##
password = ##
domainId = ##
domainName = ##
container_name = ##
conn = swiftclient.Connection(key=password,
authurl=auth_url,
auth_version='3',
os_options={"project_id": projectId,
"user_id": userId,
"region_name": region})
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
file = request.files['InputFile']
file_name = file.filename
conn.put_object(container_name,
file_name,
contents=file_name.read(),
content_length=1024)
return '''
<html>
<title>Upload new File</title>
<body>
<h1>Successful</h1>
</body>
</html>
'''
try:
print("Start serving at port %i" % PORT)
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
我缺少的东西? 我仍然得到
错误响应错误代码404。消息:找不到文件。错误代码解释:404 =没有匹配给定的URI。
,给行动=“”再试一次,例如<形式行动=“” method = post enctype = multipart/form-data> –
它仍然给我同样的错误。 –
您似乎没有运行Flask服务器,只是'httpd'。另外,你正在给一个文件名调用'.read()',这没有任何意义。 – davidism