truly working multipart uploads.

Tim Roberts timr at probo.com
Tue Nov 11 03:20:46 EST 2003


Hunter Peress <hfastjava at yahoo.com> wrote:

>I have been unable to write a script that can do (and receieve) a multipart form upload.
>
>Also, it seems that there really are differences between python's implementation and else's. 
>
>Can someone please prove me wrong with a script that works with itself AND with example 18-2
>from php:  http://www.php.net/manual/en/features.file-upload.php

Plop this script into a CGI directory somewhere, load it into your browser
and do the upload.  Use a text file; it echos the file back out to your
browser.  This uses the exact form from the php.net web page.


#! /usr/local/bin/python

import os
import cgi

q = cgi.FieldStorage()

if not q.has_key('userfile'):
  print """\
Content-Type: text/html

<html><head><title>Testing</title></head>
<body>
<form enctype="multipart/form-data" action="x.py" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
Send this file: <input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
</body>
</html>"""

else:
  f1 = q['userfile'].filename
  if q['userfile'].file:
    contents = q['userfile'].file.read()
  else:
    contents = q['userfile'].value

  print "Content-Type: text/plain"
  print
  print "Filename = ", f1
  print "Here's the content:"
  print contents

-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.




More information about the Python-list mailing list