Download file and redirect (or redirect and auto-download file)

1,129 views
Skip to first unread message

Wes Hall

unread,
Nov 9, 2011, 1:17:39 PM11/9/11
to web...@googlegroups.com
I'm afraid I'm missing something very basic here.

When a user clicks a link to download a file, how to start the download after directing to the thank-you page? I can do either, but have not figured out how to do both.

Obviously, this won't work, but the desired behavior is something like this.

controller
========
def thank_you():
    file = request.args[0]
    downloader_function(file) # response in downloader_function terminates thank_you function
    message = 'file downloaded'
    return dict(message = message)

session.flash should work for a short message, but I would like a page. My attempts at LOAD() have resulted in the download file appearing in the created div, not downloading.

Thanks

Richard Vézina

unread,
Nov 9, 2011, 5:27:34 PM11/9/11
to web...@googlegroups.com
You can use response.flash that will appear in your next page or redirection page so you get best of both world you get your message deliver and you bring your user faster to the next task to do...

If you don't like this approach I think your problem is more about usage of LOAD() and redirect... The result of you thank_you function an other page so depend of you need you will ajax trap or not your page in the same component so the initial function that redirect on thank_you will display in the component no page reload...

But I pretty sure that you are try to do your thank_you function in the first place because you are not aware of response.flash() possibility.

Hope it helps

Richard

Wes Hall

unread,
Nov 9, 2011, 8:47:05 PM11/9/11
to web...@googlegroups.com
Unless I'm missing something, session.flash() and response.flash() will not load a new page, only flash a message. Am I wrong?

If I try to load the thank_you view and then redirect to the download, it doesn't load the page, but goes straight to the download. (a response exits the function, right?)
If I try to use LOAD() the download string gets inserted into a DIV on the current page.
Using response.meta['refresh'] = "3;url=http://..../downloader" doesn't work

Apparently I'm doing something wrong with LOAD(). Is it possible to do a true redirect via LOAD()?

Anthony

unread,
Nov 10, 2011, 8:00:17 AM11/10/11
to web...@googlegroups.com
I don't think this is a web2py-specific issue. Check the internet for solutions: http://stackoverflow.com/questions/156686/how-to-start-automatic-download-of-a-file-in-internet-explorerhttp://www.willmaster.com/blog/automation/one-link-download-and-redirect.phphttp://bytes.com/topic/asp-net/answers/335502-how-redirect-after-download-starts, etc.

Not sure what you're trying to do with LOAD() -- maybe show some code.

Anthony

Wes Hall

unread,
Nov 15, 2011, 12:56:10 AM11/15/11
to web...@googlegroups.com
You're right.  In short, I was just looking for the typical, easy web2py method for http-equiv; something similar to response.meta.refresh().

I think if I spend some more time wrapping my head around response, I'll be able to better determine what I should do. In simple terms, I want to issue multiple responses, but this is probably, and rightfully so, frowned upon.

Massimo Di Pierro

unread,
Nov 15, 2011, 1:21:17 AM11/15/11
to web2py-users
This may help

http://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect

I think the "easiest" solution is to have a page with an iframe. The
iframe is hidden (size zero) and links the file to download (this will
initiate the download automatically). The page containing the iframe
can have a <meta http-equiv="refresh" content="5;url=http://..."> to
redirect to the next page while downloads continue.

Massimo
Reply all
Reply to author
Forward
0 new messages