There is a problem with the following code line
>             open(path,'w').write(request.vars.filename.file.read())

It creates the file 'a_dummy_file_name.xls' but it is not the same
size as the original file, for example:
6790KB original.xls
6862KB a_dummy_file_name.xls

If I try to open the dummy file with excel or open office it says the
file is corrupt.
I've tried to copy the file manually right after this code line and
continued execution from there onward...rest works nicely.

I tried on windows and on ubuntu, seems the same problem.

Any idea how to solve that issue with the corrupt file (=getting a
exact copy) ?


thanks,
hans


On May 7, 10:56 pm, mdipierro <[email protected]> wrote:
> This should do
>
>  def excel_read():
>     result={}
>     if request.vars.filename != None:
>         try:
>             import xlrd
>             path=os.path.join
> (request.folder,'private','a_dummy_file_name.xls')
>             open(path,'w').write(request.vars.filename.file.read())
>             book = xlrd.open_workbook(path)
>             result['The number of worksheets is']=book.nsheets
>             result["Worksheet name(s)"]= book.sheet_names()
>             sh = book.sheet_by_index(0)
>             result['sh.name']=sh.name
>             result['sh.nrows']=sh.nrows
>             result['sh.ncols']=sh.ncols
>             result['Cell D30 is']=sh.cell_value(rowx=29, colx=3)
>             for rx in range(sh.nrows):
>                 result['row ' + str(rx)]=sh.row(rx)
>         except:
>             response.flash = T('excel_read exception')
>     return dict(result=result)
>
> for you request.vars.filename is a cgi.FieldStorage object. It is not
> a named file.
> It containes
> - request.vars.filename.file (a buffered input stream)
> - request.vars.filename.filename (the original filename)
> For you to access it you should read the stream and save it with a
> name. Do not use the original filename to avoid directory traversal
> attacks.
>
> Massimo
>
> On May 7, 2:58 pm, Hans <[email protected]>
> wrote:
>
> > I'm trying to import some content directly from an excel file which
> > the user selects from its local files. I've taken and modified code
> > from the admin controller for retrieving the path/filename thru a nice
> > file selection window.
>
> > The problem: I get only the filename of a locked temporary file, not
> > the original filename. If I close the temporary file, to remove the
> > lock, the file is automatically deleted...
>
> > I would not mind to work with a temporary copy of the original file,
> > but retrieving the original path/filename thru a similar file
> > selection window would be prefered. How can this be achieved?
>
> > Hans
> > P.S. I'm using the xlrd module fromhttp://pypi.python.org/pypi/xlrd
>
> > ### viewer ###
> > {{extend 'layout.html'}}
> > <h1>excel_read.html template</h1>
>
> > <div class='frame'>
> > <h2>{{=t2.T('Select File')}}</h2>
> > {{=FORM('File: ',INPUT(_type='file',_name='filename'),INPUT
> > (_type='submit', _value='submit'))}}
> > </div>
>
> > <div class='frame'>
> > {{=BEAUTIFY(response._vars)}}
> > </div>
>
> > ### controller ###
> > def excel_read():
> >     result={}
> >     if request.vars.filename != None:
> >         try:
> >             import xlrd
>
> > # >>> can't locate the originally selected path/filename in
> > request.vars.filename <<<
> >             book = xlrd.open_workbook(request.vars.filename.file.name)
> > # >>> with a valid path/filename it works <<<
> >             book = xlrd.open_workbook('F:/Documents and Settings/Hans/
> > Desktop/test.xls')
>
> >             result['The number of worksheets is']=book.nsheets
> >             result["Worksheet name(s)"]= book.sheet_names()
> >             sh = book.sheet_by_index(0)
> >             result['sh.name']=sh.name
> >             result['sh.nrows']=sh.nrows
> >             result['sh.ncols']=sh.ncols
> >             result['Cell D30 is']=sh.cell_value(rowx=29, colx=3)
> >             for rx in range(sh.nrows):
> >                 result['row ' + str(rx)]=sh.row(rx)
> >         except:
> >             response.flash = T('excel_read exception')
> >     return dict(result=result)
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" 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/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to