Also, you can set the X-Sendfile header if your webserver supports it.

Basically, you set this header as the location of the file you want to
serve, the webserver will then discard everything in response.body,
and serve the file itself. You get the benefits of back-end
authorization, and the speed of your web server doing the streaming.

--
Thadeus





On Mon, Apr 19, 2010 at 9:31 AM, Dragonfyre13 <dragonfyr...@gmail.com> wrote:
> That's much better. Thanks!
>
> On Apr 16, 2:04 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>> never hardcode a path:
>>
>> import os
>>
>> return
>> response.stream(os.path.join(request.application,'uploads',rows[0].filedata 
>> ),'rb'))
>>
>> response stream makes sure that RANGE request are also supported.
>>
>> On Apr 16, 12:57 pm, Dragonfyre13 <dragonfyr...@gmail.com> wrote:
>>
>>
>>
>> > updated that one line to this, but it's not any better, just slightly
>> > more portable. Any insights?
>>
>> > return open("./applications/" + request.application + "/uploads/" +
>> > rows[0].filedata).read()
>>
>> > On Apr 16, 12:49 pm, Dragonfyre13 <dragonfyr...@gmail.com> wrote:
>>
>> > > I want to be able to stream a file that's uploaded via an upload
>> > > field. So far, I haven't really found a way to do this, beyond
>> > > grabbing the file name out of filedata, and then sticking static
>> > > directory path on the front, which is both kludgy, and I have to think
>> > > the wrong way of going about it. Here's what I've got:
>>
>> > > def play():
>> > >     response.headers["Content-Type"] = "audio/x-vox"
>> > >     response.headers["Content-Disposition"] = "attachment; filename="
>> > > + request.args[0] + '.vox'
>> > >     rows = db(db.audio_files.playname ==
>> > > request.args[0]).select(db.audio_files.ALL)
>> > >     if len(rows):
>> > >         return open("/opt/web2py/applications/myapp/uploads/" +
>> > > rows[0].filedata).read()
>> > >     else:
>> > >         raise ValueError, "Passed incorrect playname: " +
>> > > str(request.args[0])
>>
>> > > Essentially, I have another controller uploading to the DB under
>> > > audio_files, and sticking a "playname" in the database as a reference
>> > > to that file. I can then pass the "playfile" name into this function
>> > > in order to get it to return the data out of that file handed back.
>>
>> > > Here's the problem. Right now, I've got the directory path prepended
>> > > statically. That's bad in a big way. Second, there's no sanitation,
>> > > and minimal error handling. That will be cleaned up once I get it
>> > > working with an actual solution, rather than this hackish thing.
>>
>> > > Basically, I expect that there's something other than
>> > > response.download that will give me the actual data for a file. I
>> > > tried response.download, but it doesn't let me set the content-type,
>> > > or content-disposition (that's all set by it, overriding my header
>> > > info). There has to be something that says "take the data from this
>> > > database entry, as a file object", I'm just looking for what that is.
>>
>> > > I then want to be able to store back into it, but that's another point
>> > > not covered above, and I would assume I can just overwrite the old
>> > > file data, or just update the field in the DB. Perhaps if someone is
>> > > tackling the previous question, they can give this one a go too?
>>
>> > > --
>> > > Subscription 
>> > > settings:http://groups.google.com/group/web2py/subscribe?hl=en
>

Reply via email to