By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

Setting a Limit to the Maximum Size of an Upload

P: n/a
Hello,
I'm designing a small "briefcase" program that will allow me to quickly
upload, download, and delete files in a briefcase. The only real
things that I have left to do are to design a method for checking if
the file exists, preventing it from overwriting files from other
directories, and setting a user-configurable maximum limit on the
file's size. The former two, I can handle by myself with no problem.
However, the I'm having a little trouble with.

thefile = params["upfile.file"]
if os.path.getsize(thefile) <= conf["upmax"]:
print "File Size Okay." #Add Functions Later...
else:
print "File Too Large." #Here, too.

CGItb reported the following error:
TypeError: coercing to Unicode: need string or buffer, instance found
args = ('coercing to Unicode: need string or buffer, instance
found',)

This seems like an incredibly simple problem, but I just can't seem to
wrap my mind around it. Thank you for your help.

Oct 24 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Joey C." wrote:
thefile = params["upfile.file"]
if os.path.getsize(thefile) <= conf["upmax"]:
print "File Size Okay." #Add Functions Later...
else:
print "File Too Large." #Here, too.

CGItb reported the following error:
TypeError: coercing to Unicode: need string or buffer, instance found
args = ('coercing to Unicode: need string or buffer, instance
found',)


cgitb probably also reported what line you got that error for, and the values
of the variables involved. can you perhaps post that information too?

</F>

Oct 24 '05 #2

P: n/a
Here is a basic overview of the variables included there.

params = cgi.FieldStorage()
I accidentally made a mistake when typing what the "thefile" variable
is.
thefile = params["upfile"].file
"upfile" is the CGI field that contains the file that I'm uploading.
As you can see, the if statement just compares two values,
os.path.getsize(thefile) and conf["upmax"], a variable I set that is
designated as the maximum file size allowed.

I'm assuming that this is all the information you need. I'm sorry for
not including it earlier; I was in a bit of a rush. ^.^

Oct 24 '05 #3

P: n/a
"Joey C." wrote:
Here is a basic overview of the variables included there.

params = cgi.FieldStorage()
I accidentally made a mistake when typing what the "thefile" variable
is.
thefile = params["upfile"].file
"upfile" is the CGI field that contains the file that I'm uploading.
As you can see, the if statement just compares two values,
os.path.getsize(thefile) and conf["upmax"], a variable I set that is
designated as the maximum file size allowed.

I'm assuming that this is all the information you need. I'm sorry for
not including it earlier; I was in a bit of a rush. ^.^


and I'm sorry for not noticing the error: os.path.getsize takes a filename,
but the file attribute contains a file handle, and that UnicodeError is what
you get if you pass the wrong thing to os.path.getsize. e.g:
os.path.getsize(None) < 1000

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python23\lib\ntpath.py", line 228, in getsize
return os.stat(filename).st_size
TypeError: coercing to Unicode: need string or buffer, NoneType found

(here, the full traceback reveals that the problem is inside getsize, and not
in the comparision. the error message itself isn't exactly helpful, though...)

the file handle is usually a real (but temporary) file, so you should be able to
get the size by seeking to the end of the file:

file = params["upfile"].file
file.seek(0, 2)
size = file.tell()
file.seek(0) # rewind

inspecting the headers attribute might also help.

</F>

Oct 25 '05 #4

P: n/a
I'm afraid on my interpreter, this works.
if os.path.getsize("C:\\Documents and Settings\\Joey\\Desktop\\file.txt") <= 1000:
print "<= 1000."


<= 1000.

No problems there, as you can see.

Oct 25 '05 #5

P: n/a
Oh, I'm sorry, I didn't understand what you meant at first.
Then I read your reply over again and noticed that you said that the
problem lied in os.path.getsize() when I tried to run it on the
contents of an open file.

I'll try the method you outlined now.

Oct 25 '05 #6

P: n/a
"Joey C." wrote:
I'm afraid on my interpreter, this works.
if os.path.getsize("C:\\Documents and Settings\\Joey\\Desktop\\file.txt") <= 1000:
print "<= 1000."


<= 1000.

No problems there, as you can see.


I'm not sure if you replied to my post, but what I tried to say is that

params["upfile"].file

doesn't return a string, it returns a file object. and os.path.getsize
on something that's not a string gives the TypeError you're seeing.

</F>

Oct 25 '05 #7

P: n/a
Yes, I see that now. I tried your method and it seemed to work fine
until I tried printing the filesize out.

def checkfilesize(thefile):
# Check the Size of the File
global filesize
thefile.seek(0,2)
filesize = thefile.tell()
thefile.seek(0)
print filesize
print conf["upmax"]
if filesize <= conf["upmax"]:
print "File Size Okay."
noupload = False
else:
print "File is too Large."
noupload = True

Basically conf["upmax"] is a number that I extract from a configuration
file that is the maximum size of an upload. I had tried setting
conf["upmax"] to 1 and it should have technically disallowed a 28 byte
file to pass through. But it did, so I added two lines to print the
file size and the conf["upmax"] variable.
The file size turned out to be "0"!
thefile still is just params["upfile"].file, by the way.

Any suggestions?

Oct 27 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.