0

I am using web2py for my college project.

I have managed to upload a video into my database (by uploading it into 'uploads files').

But the problem is I don't know how to access that uploaded video afterwards.

Can someone explain me how can I access a video after it has been uploaded?

The intention is that user can see their videos and download them.

db.define_table('product',Field('TITLE',requires=IS_NOT_EMPTY()),
            Field('OWNER_ID','reference auth_user',readable=False,),
            Field('Video','upload'),
            Field('DESCRIPTION','text'),
            Field('times','datetime'),
            Field('RATE')
            )
  • 1
    Please include the code for the video upload part of your project. That will be crucial in helping us explain how to solve the problem. – stef Oct 03 '15 at 14:40
  • Under `Field('Vide0', 'upload'),`, is that `0` supposed to be there? – ASCIIThenANSI Oct 03 '15 at 15:26

1 Answers1

0

The upload field stores the name of the file, so to get the path to a file, you can do:

import os
row = db(db.product.id == some_id).select().first()
filepath = os.path.join(request.folder, 'uploads', row.Video)

However, web2py includes a built-in method, response.download that will automatically retrieve and stream a file that has been uploaded via an upload field. The scaffolding application includes the following function in the default.py controller:

def download():
    """
    allows downloading of uploaded files
    http://..../[app]/default/download/[filename]
    """
    return response.download(request, db)

To generate a link to download via the above action, you would use the URL helper as follows:

URL('default', 'download', args=row.Video)

where row is a row from the db.product table. The response.download method will read the filename from request.args[0] (i.e., the first URL arg). The filename includes the db table and field name, so response.download uses that information to identify the upload field definition, which includes the upload file path. It then streams the file.

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • Thanks for your information . Can you please tell me in which filed i have to import 'os' – HARISH DHULIPALLA Oct 06 '15 at 04:32
  • Import `os` in the file where you use it. – Anthony Oct 06 '15 at 11:39
  • Thanks again :) . But it is showing some error like "row" and "some_id" was undeclared. – HARISH DHULIPALLA Oct 07 '15 at 15:57
  • `some_id` is just a placeholder in this sample code -- you must provide the ID of a product record somehow. Regarding the `URL()` helper, again, you are responsible for creating the `row` object referenced in `args=row.Video`. These items are beyond the scope of your question, and there is no way to know exactly how they should be defined without more context. – Anthony Oct 07 '15 at 19:37