Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 23rd, 2005, 1:15 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Dec 23rd, 2005, 5:01 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Dec 23rd, 2005, 11:23 AM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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 y

Some 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:
Originally Posted by Before
ÿØÿà JFIF  ` ` ÿÛ C  

 $.' ",#(7)
Quote:
Originally Posted by After
ÿØÿà JFIF  ` ` ÿÛ C ttn r  $.' ",#(7)

Last edited by Sane; Dec 23rd, 2005 at 11:36 AM.
Sane is offline   Reply With Quote
Old Dec 23rd, 2005, 1:17 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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()
Does it for you?

Last edited by Arevos; Dec 23rd, 2005 at 1:30 PM.
Arevos is offline   Reply With Quote
Old Dec 23rd, 2005, 1:53 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
".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!
Sane is offline   Reply With Quote
Old Dec 23rd, 2005, 2:06 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Sane
OMG AHHH... MAKES SO MUCH SENSE!!!
Often the way

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.
Arevos is offline   Reply With Quote
Old Dec 24th, 2005, 1:15 AM   #7
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Check out the result of your help Arevos in the Completed Projects Section.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:32 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC