check uploaded file's file size?

Discussion in 'Python' started by h3m4n, Jul 8, 2006.

  1. h3m4n

    h3m4n Guest

    i have a short script that allows users to upload files, but when i try
    to check for a valid filesize (using fileitem) i get '-1' i can't find
    information about using filesize anywhere. any ideas?

    code:

    form = cgi.FieldStorage()
    fileitem = form["datafile"]
    print str(fileitem.filesize)

    -h3m4n
     
    h3m4n, Jul 8, 2006
    #1
    1. Advertising

  2. h3m4n

    Simon Forman Guest

    h3m4n wrote:
    > i have a short script that allows users to upload files, but when i try
    > to check for a valid filesize (using fileitem) i get '-1' i can't find
    > information about using filesize anywhere. any ideas?
    >
    > code:
    >
    > form = cgi.FieldStorage()
    > fileitem = form["datafile"]
    > print str(fileitem.filesize)
    >
    > -h3m4n


    Try this

    fileitem.seek(0, 2) # Seek to the end of the file.
    filesize = fileitem.tell() # Get the position of EOF.
    fileitem.seek(0) # Reset the file position to the beginning.

    If the fileitem is a normal file this should work.


    Peace,
    ~Simon
     
    Simon Forman, Jul 8, 2006
    #2
    1. Advertising

  3. h3m4n

    h3m4n Guest

    > Try this
    >
    > fileitem.seek(0, 2) # Seek to the end of the file.
    > filesize = fileitem.tell() # Get the position of EOF.
    > fileitem.seek(0) # Reset the file position to the beginning.


    thanks for the info. i just had to change it from fileitem to
    fileitem.file, e.g. fileitem.file.seek(0, 2), and it worked fine.

    - h3m4n
     
    h3m4n, Jul 8, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt G
    Replies:
    1
    Views:
    1,177
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. Johnson

    Limiting Uploaded File Size

    Johnson, Feb 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    520
    Lau Lei Cheong
    Feb 19, 2005
  3. RobM
    Replies:
    0
    Views:
    639
  4. Replies:
    2
    Views:
    137
  5. Justin C

    Re: Ascertaing uploaded file size

    Justin C, Apr 24, 2013, in forum: Perl Misc
    Replies:
    5
    Views:
    225
    J├╝rgen Exner
    Apr 27, 2013
Loading...

Share This Page