Hey guys, Here's an easy way to save >1m files without concern for the
filesize...code below is for PDF files, but you can change the content
type to whatever you want.

I over-commented for you... <grin>
Brian


class pdfFile( db.Model ):
        #name of the file you are working with (not unique)
        filename = db.StringProperty()
        #time the file was uploaded
        uploadedOn = db.DateTimeProperty(auto_now_add=True)

        def storePDF( self, thePDF ):
                # the pieceSize could be 1000000, but I gave 50k overhead for 
good
measure
                pieceSize = 950000
                # loop to generate all the pieces
                for i in range( 0, len( thePDF ), pieceSize ):
                        # get a new data record
                        thisPiece = pdfFilePart()
                        # the key keeps them together
                        thisPiece.pdfFile = self.key()
                        # the partNum keeps them in order
                        thisPiece.portionNum = i/pieceSize
                        # save this portion of the file to the blob
                        thisPiece.portion = thePDF[ i: i+pieceSize ]
                        # commit it
                        thisPiece.put()

        def loadPDF( self ):
                # the results will be in thePDF
                thePDF = ''
                # get all the pieces (remember that self will be the record you 
are
working with)
                q = db.GqlQuery("SELECT * FROM pdfFilePart WHERE pdfFile = :1 
ORDER
BY portionNum", self.key() )
                # technically, fetching 11 is fine because there's a 10 meg 
limit
right now 950000 x 11 > 10 meg
                results = q.fetch(11)
                # cycle for each record retreived
                for each in results:
                        # add this blob to the overall
                        thePDF += each.portion
                # return the entire file
                return thePDF

class pdfFilePart( db.Model ):
        pdfFile = db.ReferenceProperty()        # the key to the parent
        portionNum = db.IntegerProperty()       # the order of parts
        portion = db.BlobProperty()                     # this portion of the 
file


class test ( webapp.RequestHandler ):
        def get(self):
                # if "getFile" exists, then user had clicked to download the 
file
                if self.request.get('getFile'):
                        # get the key from the querystring, get that record 
from pdfFile
and store it in theFile
                        theFile = pdfFile().get( 
db.Key(self.request.get('getFile')) )
                        # tell browser this is a pdf file
                        self.response.headers['Content-Type'] = 
"application/pdf"
                        # give browser the content of the file ( 
theFile.loadPDF() )
                        self.response.out.write( theFile.loadPDF(  ) )
                else:
                        # query for all the files we currently have
                        q = db.GqlQuery("SELECT * FROM pdfFile ORDER BY 
uploadedOn")
                        # get those files
                        results = q.fetch(100)
                        # loop through each result
                        for each in results:
                                # give a link to the file (using the key) and 
displaying the
filename (remember, filename is not unique...uniqueness is from key
() )
                                self.response.out.write('<h2><a 
href="/test/?getFile=' + str
(each.key()) + '">' + each.filename + '</a></h2>')

                        # display a form to upload a file (pdfFile), and a 
filename (just
to allow you to identify the file...a non-unique filename)
                        self.response.out.write("""<form action="/test/" 
enctype="multipart/
form-data" accept-charset="utf-8" accept="application/pdf"
method="post">
                                        <input type="hidden" name="cmd" 
value="uploadPDF">
                                        <table>
                                                <tr><td>PDF 
File:</td><td><input name="pdfFile" type="file"
size="70"></td></tr>
                                                
<tr><td>Filename:</td><td><input type="textbox" name="title"
size="70"></td></tr>
                                                <tr><td colspan=2><input 
type="submit" value="upload"></td></tr>
                                        </table></form>""")

        def post(self):
                # the file was posted so get a record ready
                theFile = pdfFile()
                # set the filename
                theFile.filename = self.request.get('title')
                # putting it gives the record a KEY so we can move on to the 
next
stage
                theFile.put()
                # save the portion(s) of the file by giving the entire file to
storePDF
                theFile.storePDF( self.request.get('pdfFile') )
                # go back to the get...as opposed to self.redirect('/')
                self.get()
-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.


Reply via email to