2016-06-08 51 views
-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。

+0

,给行动=“”再试一次,例如<形式行动=“” method = post enctype = multipart/form-data> –

+0

它仍然给我同样的错误。 –

+0

您似乎没有运行Flask服务器,只是'httpd'。另外,你正在给一个文件名调用'.read()',这没有任何意义。 – davidism

回答

-1

就像你正在使用烧瓶我认为最好的解决办法是像一个瓶中的解决方案:

def upload_file(): 
    if request.method == 'POST': 
     # check if the post request has the file part 
     if 'file' not in request.files: 
      flash('No file part') 
      return redirect(request.url) 
     file = request.files['file'] 
     # if user does not select file, browser also 
     # submit a empty part without filename 
     if file.filename == '': 
      flash('No selected file') 
      return redirect(request.url) 
     if file and allowed_file(file.filename): 
      filename = secure_filename(file.filename) 
      file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 
      #return to the file or what do you want 
      return redirect(url_for('uploaded_file', 
            filename=filename)) 
在你的表单标签