CGI File Uploads and Progress Bars

2005-09-07 Thread Doug Helm
Hey, Folks:

I'm writing a CGI to handle very large file uploads.  I would like to
include a progress bar.  I think I'm about done.  I have code to handle the
file upload, and I think I can add an IFrame to my page which posts to check
file size (so I can tell how many bytes have been received).  My problem is
that I want to provide a % complete.  In order to do that, of course, I need
to know not only the number of bytes received, but also the total number of
incoming bytes.  Here's the heart of the code:

while afcommon.True:
  lstrData = lobjIncomingFile.file.read(afcommon.OneMeg)
  if not lstrData:
break
  lobjFile.write(lstrData)
  llngBytes += long(len(lstrData))
lobjFile.close()

Assume that lobjIncomingFile is actually a file-type element coming from
CGI.FieldStorage.  It's already been tested to ensure that it is a file-type
element.  Also, assume that I've already opened a file on the server,
referred to by lobjFile (so lobjFile is the target of the incoming data).

If this were a client application opening a file, I would just do the
following:

import os
print os.stat('myfile.dat')[6]

But, of course, this isn't a local file.  In fact, it's not really a file at
all.  It is the contents of a file already rolled up into the HTTP header of
the incoming HTTP request to the Web server.  The CGI module is kind enough
to handle all of the really hard stuff for me (like unpacking and decoding
the header contents, etc.).  But, I still need to know the size of the
incoming file data.

Of course, I could do this by reading the whole thing into a string variable
and then testing the length of the string, as follows:

s = lobjIncomingFile.file.read()
SizeOfFileIs = len(s)

But that really defeats the purpose, since my whole goal here is to provide
a progress bar, which is contingent upon a "chunking" approach.  Besides,
for the file sizes that I'll be dealing with, I surely wouldn't want to read
the whole thing into memory.

So, bottom line: Does anyone know how to get the size of the incoming file
data without reading the whole thing into a string?  Can I do something with
content_header?

Thanks much for any insight that you might have.

Doug


-- 
http://mail.python.org/mailman/listinfo/python-list


File Uploads

2005-03-27 Thread Doug Helm
Hey, Folks:

I'm trying to write a very simple file upload CGI.  I'm on a Windows server.
I *am* using the -u switch to start Python for CGIs, as follows:

c:\python\python.exe -u %s %s

I *do* have write permissions on the directory I'm trying to write to.  But,
when I click submit, it just hangs.  Any help would be greatly appreciated.
Thanks.  Here's the code...

Upload.py

import cgi

print "content-type: text/html\n\n"

form = cgi.FieldStorage()
if not form:
  print """









"""
else:
  import BLOB
  lobjUp = BLOB.BLOB()
  if lobjUp.Save('filename', 'SomeFile.jpg'):
print """



  File successfully saved.


"""
  else:
print """



  Unable to save file.


"""

--

Blob.py

import cgi
import staticobject

cTrue = 1
cFalse = 0

try:
  import msvcrt,os
  msvcrt.setmode( 0, os.O_BINARY ) # stdin  = 0
  msvcrt.setmode( 1, os.O_BINARY ) # stdout = 1
except ImportError:
  pass


class BLOB(staticobject.StaticObject):

  def __init__(self):
self.initializing = cTrue
staticobject.StaticObject.__init__(self)
self.initializing = cFalse

  def Save(self, pstrFormFieldName, pstrFilePathAndName):

# tried this first -- same result -- just hangs...
#try:
#  form = cgi.FieldStorage()
#  item = form[pstrFormFieldName]
#  if item.file:
#data = item.file.read()
#f = open(pstrFilePathAndName,'wb')
#f.write(data)
#f.close()
#return cTrue
#  else:
#return cFalse
#except:
#  return cFalse

form = cgi.FieldStorage()
f = open(pstrFilePathAndName,'wb')
f.write(form[pstrFormFieldName].value)
f.close()


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File Uploads

2005-03-27 Thread Doug Helm
Thanks, Dimitri.  Yes, I found that same code too and tried it with the
exact same result as the code I've uploaded (just hangs).  But, OK.  You
have it working, so it must be a systems issue.  Are you also on a Windows
IIS web server?  Do you have CGI configured the same way (i.e. .py =
python.exe -u %s %s)?

Thanks.

Doug

"dimitri pater" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Maybe this helps:
> http://www.voidspace.org.uk/python/cgi.shtml#upload
>
> I use it, it works for fine me
> Maybe it will give you some clues on how to tweak your own script.
>
> Dimitri
>
>
> On Sun, 27 Mar 2005 10:32:20 -0700, Doug Helm <[EMAIL PROTECTED]>
wrote:
> > Hey, Folks:
> >
> > I'm trying to write a very simple file upload CGI.  I'm on a Windows
server.
> > I *am* using the -u switch to start Python for CGIs, as follows:
> >
> > c:\python\python.exe -u %s %s
> >
> > I *do* have write permissions on the directory I'm trying to write to.
But,
> > when I click submit, it just hangs.  Any help would be greatly
appreciated.
> > Thanks.  Here's the code...
> >
> > Upload.py
> >
> > import cgi
> >
> > print "content-type: text/html\n\n"
> >
> > form = cgi.FieldStorage()
> > if not form:
> >   print """
> > 
> > 
> > 
> >  > enctype="multipart/form-data">
> > 
> > 
> > 
> > 
> > 
> > """
> > else:
> >   import BLOB
> >   lobjUp = BLOB.BLOB()
> >   if lobjUp.Save('filename', 'SomeFile.jpg'):
> > print """
> > 
> > 
> > 
> >   File successfully saved.
> > 
> > 
> > """
> >   else:
> > print """
> > 
> > 
> > 
> >   Unable to save file.
> > 
> > 
> > """
> >
> > --
> >
> > Blob.py
> >
> > import cgi
> > import staticobject
> >
> > cTrue = 1
> > cFalse = 0
> >
> > try:
> >   import msvcrt,os
> >   msvcrt.setmode( 0, os.O_BINARY ) # stdin  = 0
> >   msvcrt.setmode( 1, os.O_BINARY ) # stdout = 1
> > except ImportError:
> >   pass
> >
> > class BLOB(staticobject.StaticObject):
> >
> >   def __init__(self):
> > self.initializing = cTrue
> > staticobject.StaticObject.__init__(self)
> > self.initializing = cFalse
> >
> >   def Save(self, pstrFormFieldName, pstrFilePathAndName):
> >
> > # tried this first -- same result -- just hangs...
> > #try:
> > #  form = cgi.FieldStorage()
> > #  item = form[pstrFormFieldName]
> > #  if item.file:
> > #data = item.file.read()
> > #f = open(pstrFilePathAndName,'wb')
> > #f.write(data)
> > #f.close()
> > #return cTrue
> > #  else:
> > #return cFalse
> > #except:
> > #  return cFalse
> >
> > form = cgi.FieldStorage()
> > f = open(pstrFilePathAndName,'wb')
> > f.write(form[pstrFormFieldName].value)
> > f.close()
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> --
> Please visit dimitri's website: www.serpia.com


-- 
http://mail.python.org/mailman/listinfo/python-list


File Uploads -- Windows Server

2005-03-27 Thread Doug Helm
I should have been more clear in my subject line.  I was also the poster in
the "File Uploads" topic.  I'm not having any luck getting file uploads to
work (multi-part HTML form) on a Windows server.  I'm using a very close
approximation of public domain code that I found.  I've tried a couple of
different implementations (very similar), but I am essentially using the
following test code:

http://www.voidspace.org.uk/python/cgi.shtml#upload

which does *not* work on my Windows / IIS server.  I have CGIs (py and pyc
files) configured as follows:

C:\Python\Python.Exe -u %s %s

C:\Python is (of course) where Python is installed on my machine.
-u allows for binary data to be processed (I believe)
I'm not sure what %s %s does (would be nice to know...)

Anyway, I believe I have write permissions in the directory that I'm trying
to write (and would expect an error if I didn't)...

I'm not getting any error.  I submit a multi-part form to save a file
attachment to disk, and the post just hangs.

Does anyone have any ideas on this?  Has anyone made CGI file uploads work
in a Windows / IIS environment?

Thanks much for any help that you can provide.

Doug


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File Uploads

2005-03-28 Thread Doug Helm
Andrew:

I'm a dope.  You're brilliant.  Thank you.  That worked splendidly.

Doug

<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Doug Helm wrote:
>
> > form = cgi.FieldStorage()
> >   if lobjUp.Save('filename', 'SomeFile.jpg'):
>
> > class BLOB(staticobject.StaticObject):
> >   def Save(self, pstrFormFieldName, pstrFilePathAndName):
> > form = cgi.FieldStorage()
>
> You are instantiating cgi.FieldStorage twice. This won't work for POST
> requests, because instantiating a FieldStorage reads the form data from
> the standard input stream (the HTTP request).
>
> Try to create a second one and cgi will try to read all the form data
> again; this will hang, waiting for the socket to send it a load more
> data which will not be forthcoming.
>
> When using CGI, parse the input only once, then pass the results (a
> FieldStorage object if you are using the cgi module) in to any other
> functions that need to read it.
>
> --
> Andrew Clover
> mailto:[EMAIL PROTECTED]
> http://www.doxdesk.com/
>


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File Uploads

2005-03-31 Thread Doug Helm
You're right, of course, and I do appreciate it.  I generally am calling
functions and returning strings and then printing the entire string.
For example:

def SomeFunc():
  lstrRetVal = ''
  lstrRetVal += 'Content-type: text/html\n\n'
  lstrRetVal += more HTML here...
  return lstrRetVal

Then, the calling code does:

print SomeFunc()

In this case, the extra new line character is appropriate.  Somehow, the
extra new line character slipped in on the print statement in my upload
sample code (I probably copied from a function that returns a string).  But
thanks just the same...

Just to be complete (so that no one comments about string concatenation
efficiency), in a real application I would generally use triple quotes for
HTML (or append to a list and then .join into a string at the end)...

Thanks to all for your help.

"Tim Roberts" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> "Doug Helm" <[EMAIL PROTECTED]> wrote:
>
> >Hey, Folks:
> >
> >I'm trying to write a very simple file upload CGI.  I'm on a Windows
server.
> >I *am* using the -u switch to start Python for CGIs, as follows:
> >
> >c:\python\python.exe -u %s %s
> >
> >I *do* have write permissions on the directory I'm trying to write to.
But,
> >when I click submit, it just hangs.  Any help would be greatly
appreciated.
> >Thanks.  Here's the code...
> >
> >Upload.py
> >
> >import cgi
> >
> >print "content-type: text/html\n\n"
>
> I see you got your problem solved, but you should know there is a problem
> with this line.  The print statement automatically adds an end-of-line, so
> this will actually end up producing TWO blank lines after the header.  You
> should use this:
>
> print "Content-type: text/html\n"
> --
> - Tim Roberts, [EMAIL PROTECTED]
>   Providenza & Boekelheide, Inc.


-- 
http://mail.python.org/mailman/listinfo/python-list