how to make a file download site by web.py

1,773 views
Skip to first unread message

Yunfan Jiang

unread,
Jan 8, 2011, 12:02:44 PM1/8/11
to we...@googlegroups.com
i want to make a file download site by web.py
and the file content which i store in database using blob, so how can
i response the file value to user? and how to process the mime?

--
welcom to gtalk me
http://hi.baidu.com/jyf1987

Sergei Sadovski

unread,
Jan 8, 2011, 2:21:36 PM1/8/11
to we...@googlegroups.com
I did it in such way

class Download:
def GET(self, id):
web.header('Content-Type', attachment.type) # file type
web.header('Content-disposition', 'attachment; filename='
+ attachment.file_name) # force browser to show "Save as" dialog.
return data # your blob

> and how to process the mime?

When you upload file, FieldStorage instance contains mime type in
"type" property.

> --
> You received this message because you are subscribed to the Google Groups "web.py" group.
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webpy?hl=en.
>
>

--
Best regards,
Sergei Sadovski

andrei

unread,
Jan 8, 2011, 4:39:48 PM1/8/11
to we...@googlegroups.com
This is how i do it using generator. Its better because it doesn't load the whole file into memory.

class DownloadFile(app.page):
    
    path="/(users|tenders)/(\d+)/files/(\d+)"
    
    def GET(self, obj_type, obj_id, file_id):
        doc = db.select("files", locals(), where="id=$file_id and obj_type=$obj_type and obj_id=$obj_id", limit=1)[0]
        web.header("Content-Disposition", "attachment; filename=%s" % doc.filename)
        web.header("Content-Type", doc.filetype)
        web.header('Transfer-Encoding','chunked')
        f = open(os.path.join(config.upload_dir, doc.path, doc.filename), 'rb')
        while 1:
            buf = f.read(1024 * 8)
            if not buf:
                break
            yield buf

andrei

unread,
Jan 8, 2011, 4:47:48 PM1/8/11
to we...@googlegroups.com
Sorry, I misread the post. You don't need generator if you store the whole file in database.
And to process mime i used mimetypes.guess_type.

This code i use for upload (assuming we can have many file inputs with the same name "files")

def process_upload(obj_type, obj_id):
    i = web.webapi.rawinput()
    try:
        files = i.files
        now = datetime.datetime.now()
        path = os.path.join(str(now.year), str(now.month))
        if not os.path.exists(os.path.join(config.upload_dir, path)):
            os.makedirs(os.path.join(config.upload_dir, path))
        if not isinstance(files, list):
            files = [files]
        for f in files:
            if f.filename:
                filetype, encoding = mimetypes.guess_type(f.filename)
                if not filetype:
                    flash.set(u"<p>Incorrect file format.</p>")
                    continue
                file_id = db.insert("files", title=web.safeunicode(f.filename), obj_type=obj_type, obj_id=obj_id, filetype=filetype, path=path, filename="")
                filename = ("document_%d" % file_id) + mimetypes.guess_extension(filetype)
                f2 = open(os.path.join(config.upload_dir, path, filename), 'w')
                while 1:
                    buf = f.file.read(1024 * 8)
                    if not buf:
                        break
                    f2.write(buf)
                size = f2.tell()
                f.file.close()
                f2.close()
                db.update("files", where="id=$file_id", vars=dict(file_id=file_id), filename=filename, size=size)
    except KeyError:
        pass


Yunfan Jiang

unread,
Jan 8, 2011, 8:17:58 PM1/8/11
to we...@googlegroups.com
thank you for all your help, Sergei Sadovski and andrei, i think i
got what i need
Reply all
Reply to author
Forward
0 new messages