[web2py] Re: How to make a function to download a file?
*As I see I need this:* * stream = cStringIO.StringIO() * * return stream.getvalue() * * * *I have a ready to go content as a string in a variable:* *content = * '.'* * So how to put this string in a stream? *My code is:* * * *def download(): response.headers['Content-Type'] = 'text/csv' attachment = 'attachment;filename=' + file+ '.csv' response.headers['Content-Disposition'] = attachment content = ',,' raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'})* * *
[web2py] Re: How to make a function to download a file?
On Wednesday, December 28, 2011 1:05:43 PM UTC-5, thstart wrote: *As I see I need this:* * stream = cStringIO.StringIO() * * return stream.getvalue()* Actually, that will do the same thing you've already tried (stream.getvalue() is just a string, so you'll be returning a string, just like your original code). In fact, when I try your original code, it works fine, so perhaps your problem is with the link or routing (i.e., the link isn't properly routing to the download function). Also, in your code, where does the 'file' variable come from? Anthony
[web2py] Re: How to make a function to download a file?
file is composed dynamically - I get it from request - it is send like ?file=name and I get it from there. can you post the code you just tried?
[web2py] Re: How to make a function to download a file?
I literally just copied and pasted your code into a basic 'welcome' app, except I hard-coded a filename (data.csv): def download(): response.headers['Content-Type'] = 'text/csv' attachment = 'attachment;filename=data.csv' response.headers['Content-Disposition'] = attachment content = ',,' raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'}) Then I entered that URL in my browser address bar, and I got a CSV file download, which opened right up in Excel (it included one row of data, with ... in each of the three first columns). Anthony On Wednesday, December 28, 2011 3:55:12 PM UTC-5, thstart wrote: file is composed dynamically - I get it from request - it is send like ?file=name and I get it from there. can you post the code you just tried?
[web2py] Re: How to make a function to download a file?
You mean my original code is working? With IE it is not. What browser you are using? In IE nothing comes for download.
[web2py] Re: How to make a function to download a file?
I literally copied your exact code (just had to hard-code the filename): def download(): response.headers['Content-Type'] = 'text/csv' attachment = 'attachment;filename=data.csv' response.headers['Content-Disposition'] = attachment content = ',,' raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'}) Then I entered that URL in my browser and I got the CSV file download, with the content as above. Anthony On Wednesday, December 28, 2011 3:55:12 PM UTC-5, thstart wrote: file is composed dynamically - I get it from request - it is send like ?file=name and I get it from there. can you post the code you just tried?
[web2py] Re: How to make a function to download a file?
On Wednesday, December 28, 2011 4:10:55 PM UTC-5, thstart wrote: You mean my original code is working? With IE it is not. What browser you are using? In IE nothing comes for download. Works in IE7 and IE9 for me. Are you clicking a link or going directly to /appname/controller/download in the browser? Perhaps the link is incorrect or there's a problem with the filename (though not sure that would affect the download). Also, note that the default.py controller of the 'welcome' app already includes a download() function -- if you've added a second download function to that controller, I think that will cause a problem. Anthony
Re: [web2py] Re: How to make a function to download a file?
I fired the debugger, when I click on the link, the debugger stops at the function (the name is export), it comes down to raise HTTP but does not downloads the file. On Wed, Dec 28, 2011 at 2:56 PM, Anthony abasta...@gmail.com wrote: On Wednesday, December 28, 2011 4:10:55 PM UTC-5, thstart wrote: You mean my original code is working? With IE it is not. What browser you are using? In IE nothing comes for download. Works in IE7 and IE9 for me. Are you clicking a link or going directly to /appname/controller/download in the browser? Perhaps the link is incorrect or there's a problem with the filename (though not sure that would affect the download). Also, note that the default.py controller of the 'welcome' app already includes a download() function -- if you've added a second download function to that controller, I think that will cause a problem. Anthony
Re: [web2py] Re: How to make a function to download a file?
Not sure what the problem is. What version of web2py? Can you attach a minimal app that reproduces the problem? On Wednesday, December 28, 2011 5:58:03 PM UTC-5, thstart wrote: I fired the debugger, when I click on the link, the debugger stops at the function (the name is export), it comes down to raise HTTP but does not downloads the file. On Wed, Dec 28, 2011 at 2:56 PM, Anthony abas...@gmail.com wrote: On Wednesday, December 28, 2011 4:10:55 PM UTC-5, thstart wrote: You mean my original code is working? With IE it is not. What browser you are using? In IE nothing comes for download. Works in IE7 and IE9 for me. Are you clicking a link or going directly to /appname/controller/download in the browser? Perhaps the link is incorrect or there's a problem with the filename (though not sure that would affect the download). Also, note that the default.py controller of the 'welcome' app already includes a download() function -- if you've added a second download function to that controller, I think that will cause a problem. Anthony
[web2py] Re: How to make a function to download a file?
I don't use a database to create the CSV content but generate is as a comma delimited text. Also I want to set the file name to be downloaded. I don't see how to do it from the book. This line has to be changed in order to work: raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'})
[web2py] Re: How to make a function to download a file?
Maybe try writing the content to a StringIO object and using response.stream (see http://web2py.com/books/default/chapter/29/4#response). Anthony On Thursday, December 22, 2011 10:09:04 PM UTC-5, thstart wrote: I want the visitors to be able to download a CSV file. here is my code: def download(): response.headers['Content-Type'] = 'text/csv' attachment = 'attachment;filename=' + file+ '.csv' response.headers['Content-Disposition'] = attachment content = ',,' raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'}) When I click on the link download, I get 'Undefined' in the browser and nothing is downloaded. What is wrong?
[web2py] Re: How to make a function to download a file?
I tried to achieve something similar with below link - have a look. Although I did not change the downloads() default function. It worked for me.. http://groups.google.com/group/flutuberwachung/browse_thread/thread/32b9daae6cc14840/e03404cf2d6e35b5#e03404cf2d6e35b5 Cheers Rahul On Dec 23, 9:28 pm, DenesL denes1...@yahoo.ca wrote: Look at the code samples underhttp://web2py.com/books/default/chapter/29/10#CSV On Dec 22, 10:09 pm, thstart thst...@gmail.com wrote: I want the visitors to be able to download a CSV file. here is my code: def download(): response.headers['Content-Type'] = 'text/csv' attachment = 'attachment;filename=' + file+ '.csv' response.headers['Content-Disposition'] = attachment content = ',,' raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'}) When I click on the link download, I get 'Undefined' in the browser and nothing is downloaded. What is wrong?
[web2py] Re: How to make a function to download a file?
Look at the code samples under http://web2py.com/books/default/chapter/29/10#CSV On Dec 22, 10:09 pm, thstart thst...@gmail.com wrote: I want the visitors to be able to download a CSV file. here is my code: def download(): response.headers['Content-Type'] = 'text/csv' attachment = 'attachment;filename=' + file+ '.csv' response.headers['Content-Disposition'] = attachment content = ',,' raise HTTP(200,str(content), **{'Content-Type':'text/csv', 'Content-Disposition':attachment + ';'}) When I click on the link download, I get 'Undefined' in the browser and nothing is downloaded. What is wrong?