Hopefully this is easier to read:

@service.jsonrpc
def sendFileJSON(dataFromJSON):
    #sendFileJSON


    parentpath='/Documents/'
    filename='testFile06.dat'
    #eventually use dataFromJSON instead of abc
    file = cgi.FieldStorage('file', 'testFile06.dat', 'abc\n')


    filepath=parentpath+filename
    filetype=os.path.splitext(filename)[1][1:]
    row=db((db.allfiles.parentpath==parentpath)& \
 
(db.allfiles.filepath==filepath)&(db.allfiles.filename==filename) \
        &(db.allfiles.user==me)).select()

    if not len(row):
        db.allfiles.insert(filename=filename,filepath=filepath, \
            parentpath=parentpath,filetype=filetype, \
            file=db.allfiles.file.store(file.file,filename), \
            datecreated=now,user=me)
        result = {
                'Path' : parentpath,
                'Name' : filename,
                'Error' :'',
                'Code':0
            }
    else:
        result = {
                'Path' : parentpath,
                'Name' : filename,
                'Error' : 'File already exists',
                'Code':1
            }
        return str(result)
    row=db((db.allfiles.filename==filename)& \
        (db.allfiles.user==me)).select()[0]
    insertedfile=row.file
    outfile=open(output,'wb')
    try:
        filepath=os.path.join(request.folder,'uploads',insertedfile)
        filesize=os.path.getsize(filepath)
        if filepath[-3:]=='txt' or filepath[-4:]=='html':
            filepath=os.path.normpath(filepath)
            filein=open(filepath,'r')
            dbcontent=filein.read()
            filein.close()
            db((db.allfiles.filename==filename)& \
                (db.allfiles.user==me)).update( \
                filesize=filesize,content=dbcontent)
        else:
            db((db.allfiles.filename==filename)& \
                (db.allfiles.user==me)).update( \
                filesize=filesize)
        outfile.close()
    except:
        outfile.write(str(sys.exc_info()[1]))
        outfile.close()
    return "saved"

On Feb 21, 3:54 pm, Charles Law <[email protected]> wrote:
> I'm working on a game app that is setup to have users create profiles
> using the Auth class.  I want to have users save games specific to
> their account, and allow them to have multiple saved games.  I also
> want to have the saved games listed so the user can load (and maybe
> download) them.  The files would be strings that shouldn't be more
> than a couple kb.
>
> I found this awesome example app that I'm looking through, but it
> works with uploaded files vs auto-generated files made from strings:
>  http://web2py.com/appliances/default/show/70
>
> I'm trying to extract out some of the code from this appliance and
> write data, but every file I write is blank.  I put in some debug and
> it is going through the try block, also the test.txt, which looks like
> it is debug text, is empty.  I feel like there is something small, but
> important that I'm missing, any ideas?  Here is the code I have right
> now:
>
> output=os.path.join(request.folder,'static','test.txt')
>
> ...
>
> @service.jsonrpc
> def sendFileJSON(dataFromJSON):
>     parentpath='/Documents/' #temporary
>     filename='testFile06.dat' #temporary
>     file = cgi.FieldStorage('file', 'testFile06.dat', 'abc\n')
> #eventually use dataFromJSON instead of abc
>
>     filepath=parentpath+filename
>     filetype=os.path.splitext(filename)[1][1:]
>
> row=db((db.allfiles.parentpath==parentpath)&(db.allfiles.filepath==filepath 
> )&(db.allfiles.filename==filename)
> \
>     &(db.allfiles.user==me)).select()
>
>     if not len(row):
>         db.allfiles.insert(filename=filename,filepath=filepath, \
>
> parentpath=parentpath,filetype=filetype,file=db.allfiles.file.store(file.fi 
> le,filename),datecreated=now,user=me)
>         result = {
>                 'Path' : parentpath,
>                 'Name' : filename,
>                 'Error' :'',
>                 'Code':0
>             }
>     else:
>         result = {
>                 'Path' : parentpath,
>                 'Name' : filename,
>                 'Error' : 'File already exists',
>                 'Code':1
>             }
>         return str(result)
>
> row=db((db.allfiles.filename==filename)&(db.allfiles.user==me)).select()
> [0]
>     insertedfile=row.file
>     outfile=open(output,'wb')
>     try:
>         filepath=os.path.join(request.folder,'uploads',insertedfile)
>         filesize=os.path.getsize(filepath)
>         if filepath[-3:]=='txt' or filepath[-4:]=='html':
>             filepath=os.path.normpath(filepath)
>             filein=open(filepath,'r')
>             dbcontent=filein.read()
>             filein.close()
>
> db((db.allfiles.filename==filename)&(db.allfiles.user==me)).update(filesize 
> =filesize,content=dbcontent)
>         else:
>
> db((db.allfiles.filename==filename)&(db.allfiles.user==me)).update(filesize 
> =filesize)
>         outfile.close()
>     except:
>         outfile.write(str(sys.exc_info()[1]))
>         outfile.close()
>     return "saved"

Reply via email to