473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Setting a Limit to the Maximum Size of an Upload

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
7 5651
"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
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
"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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Erik Hendrix | last post by:
Hi, I have a question here regarding setting the prefetch size. So far we took the rule that for OLTP, prefetchsize = extent size and for DSS prefetchsize = extent size * number. However,...
2
by: Kums | last post by:
What is the maximum permissible size of a database? Is there any limitation. What is the maximum # of tablespace's allowed in a database? Thanks for your response.
5
by: Sehboo | last post by:
Hi, Is there any easy way to limit the size of the string? I have Private msDescription As String I want to limit the msDescription to 50 instead of unlimitted.
3
by: Jefferis NoSpamme | last post by:
Hello all, I'm trying to limit the file size to 1 meg on upload of image files and I am trying a script from javascript internet, but it is giving me errors on IE ² is null or not an object ³...
2
by: brian.lukoff | last post by:
What is the maximum size of POST data? A page on the ASP FAQ web site (http://classicasp.aspfaq.com/forms/what-is-the-limit-on-form/post-parameters.html) is confusing me: "While GET is limited...
7
by: tomlebold | last post by:
The file size of the Access application I'm currently working on is 77,000 MB. What is the maximum size the Access file or MDB can be?
2
by: Woody Ling | last post by:
I am now using db2 v8.2 64bits without DPF. I want to create a very large table which is about 1000G and the record length is suitable for 32K page size. I find in the manual that the maximum size...
0
by: karthikbalaguru | last post by:
Hi, What could be the Maximum size of a Class ? Is there any limit on it ? Thx in advans, Karthik Balaguru
6
by: fiefie.niles | last post by:
In ASP.Net (VS 2005), is there a way to limit the user to upload a file up to certain size (say, I would like to limit the user to upload at the maximum 1 meg of file) ? Thank you
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.