| 806 | |
| 807 | |
| 808 | class SingleUploadFileApp: |
| 809 | |
| 810 | body = b""" |
| 811 | <html> |
| 812 | <head><title>form page</title></head> |
| 813 | <body> |
| 814 | <form method="POST" id="file_upload_form" |
| 815 | enctype="multipart/form-data"> |
| 816 | <input name="file-field" type="file" value="some/path/file.txt" /> |
| 817 | <input name="int-field" type="text" value="" /> |
| 818 | <input name="button" type="submit" value="single"> |
| 819 | </form> |
| 820 | </body> |
| 821 | </html> |
| 822 | """ |
| 823 | |
| 824 | def __call__(self, environ, start_response): |
| 825 | req = Request(environ) |
| 826 | status = b"200 OK" |
| 827 | if req.method == "GET": |
| 828 | body = self.body |
| 829 | else: |
| 830 | body = b""" |
| 831 | <html> |
| 832 | <head><title>display page</title></head> |
| 833 | <body> |
| 834 | """ + self.get_files_page(req) + b""" |
| 835 | </body> |
| 836 | </html> |
| 837 | """ |
| 838 | headers = [ |
| 839 | ('Content-Type', 'text/html; charset=utf-8'), |
| 840 | ('Content-Length', str(len(body)))] |
| 841 | # PEP 3333 requires native strings: |
| 842 | headers = [(str(k), str(v)) for k, v in headers] |
| 843 | start_response(status, headers) |
| 844 | assert(isinstance(body, bytes)) |
| 845 | return [body] |
| 846 | |
| 847 | def get_files_page(self, req): |
| 848 | file_parts = [] |
| 849 | uploaded_files = [(k, v) for k, v in req.POST.items() if 'file' in k] |
| 850 | for name, uploaded_file in uploaded_files: |
| 851 | if isinstance(uploaded_file, cgi.FieldStorage): |
| 852 | filename = to_bytes(uploaded_file.filename) |
| 853 | value = to_bytes(uploaded_file.value, 'ascii') |
| 854 | content_type = to_bytes(uploaded_file.type, 'ascii') |
| 855 | else: |
| 856 | filename = value = content_type = b'' |
| 857 | file_parts.append(b""" |
| 858 | <p>You selected '""" + filename + b"""'</p> |
| 859 | <p>with contents: '""" + value + b"""'</p> |
| 860 | <p>with content type: '""" + content_type + b"""'</p> |
| 861 | """) |
| 862 | return b''.join(file_parts) |
| 863 | |
| 864 | |
| 865 | class UploadBinaryApp(SingleUploadFileApp): |
no outgoing calls
searching dependent graphs…