1

Flask を使用して、ユーザーが解析してデータベースに入れる CSV をアップロードできる単一ページの Web サイトをホストすることを検討しています。データベースの悪ふざけはすべて完了し (別の Python スクリプトで SQLalchemy を使用)、スクリプトが CSV にアクセスできるようになったら、すべて解決しました。

シナリオは次のとおりです。

1. User directs browser at URL (probably something like 
   http://xxx.xxx.xxx.xxx/upload/)
2. User chooses CSV to upload
3. User presses upload
4. File is uploaded and processed, but user is sent to a thank you page while our
   script is still working on the CSV (so that their disconnect doesn't cause the
   script to abort).

CSV がサーバーに残されていればまったく問題ありません (実際、処理がうまくいかなかった場合に備えてバックアップがあるため、おそらく好まれます)。

私が欲しいのはソケットをリッスンするデーモンだと思いますが、これについてはあまり経験がなく、どこから構成またはFlaskのセットアップを開始すればよいかわかりません。

Flask 以外のフレームワークの方が簡単だと思う場合は、間違いなくお知らせください。

どうもありがとうございました!!

4

1 に答える 1

2

これは、クックブックの例に基づいて web.py でファイルのアップロードを処理する (非常に単純化された) 例です(私があまり経験していない Flash の例は、さらに簡単に見えます)。

import web

urls = ('/', 'Upload')

class Upload:
    def GET(self):
        web.header("Content-Type","text/html; charset=utf-8")
        return """
               <form method="POST" enctype="multipart/form-data" action="">
               <input type="file" name="myfile" />
               <br/>
               <input type="submit" />
               """

    def POST(self):
        x = web.input(myfile={})
        filedir = '/uploads' # change this to the directory you want to store the file in.
        if 'myfile' in x: # to check if the file-object is created
            filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
            filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
            fout = open(filedir +'/'+ filename,'wb') # creates the file where the uploaded file should be stored
            fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
            fout.close() # closes the file, upload complete.
        raise web.seeother('/')

if __name__ == "__main__":
   app = web.application(urls, globals()) 
   app.run()

これにより、アップロード フォームがレンダリングされ、(POST 時に) アップロードされたファイルが読み取られ、指定されたパスに保存されます。

于 2013-05-17T16:54:25.870 に答える