코드 양식 here을 사용하여 Python CGI 스크립트를 통해 단일 양식 필드를 사용하여 여러 파일을 업로드하고 싶습니다. 최신 브라우저는 here 및 here에 대해이 기능을 지원하는 것으로 보입니다. HTML 양식은 간단 보인다 내가 처음에 가정Python CGI를 통해 단일 양식 필드를 통해 여러 파일 업로드
file=file0&file=file1
이 될 것입니다 :
<input name="file" type="file" multiple="" />
이 file0
및 file1
은 다음 HTML5 input multiple attribute 당이 발생합니다 이름을 두 개의 파일을 선택하려면이 양식을 사용하여 정렬 배열하지만 분리에 대한 앰퍼샌드를 사용하는 것 같습니다.
코드를 수정하고 for
문을 추가하면 다음 코드를 사용하여 양식 필드에 지정된 각 파일을 반복 할 수 없습니다 (아래 오류 참조). for
문을 사용하는 것이 가장 좋은 방법이 아닌 경우 Python을 사용하여 작동 할 수있는 다른 아이디어도 있습니다.
#!/usr/bin/python
import cgi, os
form = cgi.FieldStorage()
# Generator to buffer file chunks
def fbuffer(f, chunk_size=10000):
while True:
chunk = f.read(chunk_size)
if not chunk: break
yield chunk
for fileitem in form['file']:
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
f = open('/var/www/domain.com/files' + fn, 'wb', 10000)
# Read the file in chunks
for chunk in fbuffer(fileitem.file):
f.write(chunk)
f.close()
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
단일 파일 선택 오류 :
Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/cgi-bin/test.py", line 13, in <module>, referer: https://www.domain.com/files/upload.htm
for fileitem in form['file']:, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/python2.6/cgi.py", line 518, in __iter__, referer: https://www.domain.com/files/upload.htm
return iter(self.keys()), referer: https://www.domain.com/files/upload.htm
File "/usr/lib/python2.6/cgi.py", line 583, in keys, referer: https://www.domain.com/files/upload.htm
raise TypeError, "not indexable", referer: https://www.domain.com/files/upload.htm
TypeError: not indexable, referer: https://www.domain.com/files/upload.htm
Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
두 파일 선택 오류 : .filename
참조가 제거되면, 세 번째 오류가 생성됩니다
Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/cgi-bin/test.py", line 19, in <module>, referer: https://www.domain.com/files/upload.htm
if fileitem.filename:, referer: https://www.domain.com/files/upload.htm
AttributeError: 'list' object has no attribute 'filename', referer: https://www.domain.com/files/upload.htm
Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
, 하나 또는 두 개의 파일을 동일 선택됨 :
Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
File "/usr/lib/cgi-bin/test.py", line 24, in <module>, referer: https://www.domain.com/files/upload.htm
fn = os.path.basename(fileitem), referer: https://www.domain.com/files/upload.htm
File "/usr/lib/python2.6/posixpath.py", line 111, in basename, referer: https://www.domain.com/files/upload.htm
i = p.rfind('/') + 1, referer: https://www.domain.com/files/upload.htm
AttributeError: 'list' object has no attribute 'rfind', referer: https://www.domain.com/files/upload.htm
Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
'for' 문과 새로운 해당 오류가 업데이트되었습니다. 저장 방법도 변경하도록 제안하고 있습니까? 그렇다면 for 문 아래에있는 기존 코드를 모두 제거해야합니까? 감사합니다 – Astron
@Astron : 답변을 업데이트했습니다. – jfs