[web2py] Re: Copy and insert a row in a database
On Tuesday, 8 November 2016 17:54:48 UTC, Anthony wrote: > > > The second argument to .store() should be the original filename (which > will then be encoded again), not a full file path. To get the original > filename, use the .retrieve() method instead of manually constructing the > file path and opening the file: > > filename, stream = db.name.file.retrieve(a.file) > db.name.insert(..., file=db.name.file.store(stream, filename)) > > Also, your use of a.file implies the upload field is named "file", but in > your insert code, you use "file_up" -- not sure which is correct. > > Anthony > Thanks this is exactly what I was after! (I had edited the original code down to something more readable so it should have been consistent as "file") -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] Re: Copy and insert a row in a database
> > Here is some code: > > def copy(): > import os > a = db.name(db.name.id== request.args(0)) > filepath = os.path.join(request.folder, 'uploads', a.file) > stream = open(filepath, 'rb') > db.name.insert(title= '%s_copy' % (a.title), > file_up = db.name.file.store(stream, filepath > ), > > The second argument to .store() should be the original filename (which will then be encoded again), not a full file path. To get the original filename, use the .retrieve() method instead of manually constructing the file path and opening the file: filename, stream = db.name.file.retrieve(a.file) db.name.insert(..., file=db.name.file.store(stream, filename)) Also, your use of a.file implies the upload field is named "file", but in your insert code, you use "file_up" -- not sure which is correct. Anthony -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.