1 create new app and mkdir docfiles in it

# models/db1.py
db.define_table( 'zurina',
                          Field('jenka_fnm', requires=IS_NOT_EMPTY(), 
label='orig_file_name'  ),
                          Field("fld",label="Remark", length=3500,),
                          Field('new_image_file', requires=IS_NOT_EMPTY(), 
label='w2p_file_name'  ),
                          )
# controllers/default.py

# -*- coding: utf-8 -*-

def error(message="auth ?!!!error!!!"):
    session.flash = message
    redirect(URL('index'))


def zurina():
    import os

    jenka_fnm=''
    l_id = 0

    new_image_file_path=''
    # mkdir docfiles in app dir !!!!!!!!!!!!!
    upfolder= os.path.join(request.folder, 'docfiles')

    form=SQLFORM.factory( Field('new_image_file', 'upload',uploadfolder= 
upfolder, requires=IS_NOT_EMPTY(),
                                        label='orig_file_name'  ),
                          Field("fld", 'text', label="Remark:", requires = 
IS_NOT_EMPTY(error_message='emplty field is bad!') ),
                          )

    form.element('textarea[name=fld]')['_rows']='2'


    if form.process().accepted:
          jenka_fnm = request.vars.new_image_file.filename # origin file 
name
          new_image_file_path = os.path.join(upfolder, 
form.vars.new_image_file)
          piece = dict( jenka_fnm = jenka_fnm, new_image_file = 
new_image_file_path  , fld = form.vars.fld  )
          l_id=db.zurina.insert(**db.zurina._filter_fields( piece  ))
          request.flash='Ok!!!'
          session.flash= 'ok! ok!'
          #redirect(URL('zurina_grid'))
    elif form.errors:
        response.flash = 'Please correct the error(s).'


    return locals()

def doc2user():
    import os
    prn_id = request.args(0,cast=int)
    task = db.zurina(prn_id) or error()
    qu=db.zurina.id == prn_id
    res= db(qu).select().first()
    file_path = res.new_image_file
    ext = os.path.splitext( res.jenka_fnm  )
    with open(file_path, 'rb') as f:
            file_text= f.read()

    from gluon.contenttype import contenttype
    tru_ext=''
    if len(ext) and len(ext[1]):
          tru_ext = ext[1].lower()

    if len(tru_ext) :
        response.headers['Content-Type'] = contenttype(tru_ext)
    else:
        response.headers['Content-Type'] = 
contenttype('application/octet-stream')

    if len(tru_ext) and  tru_ext.endswith(('pdf','jpeg', 'jpg', 'png', 
'bmp')):
          response.headers['Content-disposition'] = 'inline; 
filename=\"%s"' % ( res.jenka_fnm)
    else:
          response.headers['Content-disposition'] = 'attachment; 
filename=\"%s"' % ( res.jenka_fnm)

    #return "{}".format( file_path  )
    return file_text

def zurina_grid():

      query = db.zurina.id > 0

      grid=SQLFORM.grid( query, maxtextlength=200,user_signature=False, 
csv=False, buttons_placement = 'left',
           orderby =~ db.zurina.id,

           links=[ lambda row: A('Save',_href=URL('doc2user',args=row.id),  
_class="btn btn-default" , _title="save file to disk"  ), ],

           deletable = True, create=False, 
editable=False,showbuttontext=False)
      return locals()
-----------------------------------------------
# views/default/zurina.html
{{extend 'layout.html'}}

{{=A('Go to grid',_href=URL('zurina_grid'),_class="btn btn-primary 
btn-space", _title="View files list")}}

{{=form}}
# ---------- views/default/zurina_grid.html
{{extend 'layout.html'}}
{{=grid}}




четверг, 24 сентября 2020 г., 17:54:53 UTC+3 пользователь 
[email protected] написал:
>
> Hi,
> i have a controller with this :
> response.headers['Content-Disposition'] = 'inline; filename=%s' % 
> request.vars.filename   
>
> to force streaming the pdf file , and i do not why it does not work ..
> any idea ?
>
> Thank you 
>
>  
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/08b25ff4-796b-4495-9215-b99a86182eb9o%40googlegroups.com.

Reply via email to