前提・実現したいこと
フォームから選択したファイルをアップロードできるようにする。
発生している問題・エラーメッセージ
該当のソースコード
wordOperation.pyからform部分に該当する箇所を 抜粋。
python
1baseHtml1 = """ 2 <html lang="ja"> 3 <head> 4 <title>ENGLISHPRACTICE</title> 5 </head> 6 <body> 7""" 8registAllHtml = """ 9 <h1>EnglishWordRegist</h1> 10 <p>ユーザー:%s</p> 11 <form accept-charset="UTF-8" action="wordOpeComplete.py" method="post" enctype="multipart/form-data"> 12 <input type="hidden" name="operation2" value="registAll"> 13 <input type="hidden" name="wordBook" value="%s"> 14 <p>■ ファイルを選択してください。</p> 15 ファイル:<input type="file" type="file" /> 16 <br><br> 17 <input type = "submit" name="submit" value="登録"/> 18 <input type = "hidden" name="userName" value="%s"> 19 </form> 20 <form action="wordManagementWindow.py" method="get"> 21 <input type="submit" name="wordManagementWindow" value="戻る" /> 22 <input type="hidden" name="userName2" value = "%s" /> 23 </form> 24""" 25baseHtml2 = """ 26 </body> 27 </html> 28""" 29#============================================================================== 30# 本体処理 31#============================================================================== 32cgitb.enable() 33print('Content-type: text/html; charset=utf-8\r\n') 34sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 35sys.stderr = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 36 37#print('Content-type:text/html\r\n') 38 39#--- フォーム処理 40form = cgi.FieldStorage() 41userName = form.getvalue('userName') 42operation = form.getvalue('operation') 43wordBook = form.getvalue('wordBook') 44 45#--- HTML出力処理 46print(baseHtml1) 47 48elif operation == "registAll": 49 print(registAllHtml % (userName, wordBook, userName, userName)) 50 51print(baseHtml2) 52
wordOpeComplete.pyより、formからファイルを受け取った後の処理を記述したコードを 抜粋。
python
1 2cgitb.enable() 3sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 4sys.stderr = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 5 6#--- フォーム処理 7form = cgi.FieldStorage() 8 9#--- HTML出力処理 10elif operation2 == "registAll": 11 fileitem = form["file"] 12 if fileitem.file: 13 linecount = 0 14 while True: 15 print(line) 16 line = fileitem.file.readline() 17 if not line: 18 break 19 linecount = linecount + 1 20 print("ok")
補足情報(FW/ツールのバージョンなど)
python3.x
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。