![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
PyCherry Help?
Okay, for uploading files. I checked under recipes/file uploads on the cherrypy.org website. The current code is for the last version of cherrypy. I did my best to translate it by changing cpg to cherrypy, etc...
But it didn't work. It froze on the cgi.FileStorage line. So I looked at the "What's New!" logs. But the "File Upload Behaviour" link is grey and incomplete. Any ideas how to translate the file upload code to the newest version? I backtracked and tried taking the results of a file submit html form, and converting the last string from hex to decimal. But data was lost and it becomes corrupt. Ascii transfers = bad. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Could you post up the code you have to handle file uploads? Also, try searching through the cherrypy users mailing list. That usually has a lot of good advice in.
|
|
|
|
|
|
#3 | ||
|
Programming Guru
![]() |
My current code, although highly stupid using splits() and ascii transfers.
This function takes image as a result of the file input type from an html form. strimage = str(image)
imagen = strimage.split("'")[3]
imaged = strimage[27+len(imagen):-2]
if imagen:
if not (imagen.upper()).endswith('JPG') or (imagen.upper()).endswith('GIF'):
res = "Only JPG or GIF is accepted."
if res:
return redirect("%seditprofile?error=%s"%(self.domain, res))
if imagen:
f = open('avatars/%s.JPG'%(userID), 'wb')
f.write(self.hex_to_dec(imaged))
f.close() def hex_to_dec(self, hexkey):
hexset = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'a':10,'b':11,'c':12,'d':13,'e':14,'f':15}
y = ''
l1 = False
l2 = False
l3 = False
for c in hexkey:
if c == chr(92):
l1 = True
elif l1 and c == 'x':
l1 = False
l2 = True
elif l2:
l2 = False
l3 = True
num1 = hexset[c]
elif l3:
l3 = False
num2 = hexset[c]
y += chr(num2*16+num1)
else:
l1 = False
y += c
return ySome of the characters became corrupted and look different then the ascii in the original file. I'll check out the mailing list. EDIT: HOLY CRAP. I think I found my mistake... I multiplied the second number by 16, not the first!!! :mad: Grr.... didn't work. It's still corrupted, but at least it draws it on the screen this time... http://jammersbase.gotdns.com/viewprofile?userID=1 ![]() The two files aren't very different at all actually... Quote:
Quote:
Last edited by Sane; Dec 23rd, 2005 at 11:36 AM. |
||
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
This works for me:
import cherrypy
class HelloWorld:
@cherrypy.expose
def index(self):
return """
<html><body>
<form action="upload" method="post" enctype="multipart/form-data">
filename: <input type="file" name="avatar"/><br/>
<input type="submit"/>
</form>
</body></html>
"""
@cherrypy.expose
def upload(self, avatar):
output = open("avatars/" + avatar.filename, 'wb')
output.write(avatar.file.read())
output.close()
return """
<html><body>
Uploaded %s.
</body></html>
""" % avatar.filename
cherrypy.root = HelloWorld()
cherrypy.config.update(file = "config")
cherrypy.server.start()Last edited by Arevos; Dec 23rd, 2005 at 1:30 PM. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
".file.read()"
OMG AHHH... MAKES SO MUCH SENSE!!! ![]() ![]() ![]() ![]() Thanks man!! I guess splitting the string converts it to hex, this leaves it alone. Wow yay! |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
![]() They really ought to update the CherryPy site. Nice framework, but they're really behind on the documentation ![]() By the way, if you ever need to convert an binary to ascii and back again, check out the base64 module, and base64.b64encode and base64.b64decode. |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() |
Check out the result of your help Arevos in the Completed Projects Section.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|