Thanks,
Yeah it figures it would be something dumb on my part like that. :s
I'm actually trying to port an old site I've made over a couple of years
back...
So, basically on that js I processed everything I wanted out of the page
and then...
(...)
var hidden_list = document.createElement('input');
hidden_list.type = 'hidden';
hidden_list.id = 'protlist';
hidden_list.name = 'protlist';
hidden_list.value = tsv;
var save_as = document.createElement('input');
save_as.type = 'hidden';
save_as.id = 'save_as';
save_as.name = 'save_as';
save_as.value = file_name;
form.appendChild(hidden_list);
form.appendChild(save_as);
form.action = '/path/to/cgiscript/saver.py'
form.submit();
form.removeChild(hidden_list);
form.removeChild(save_as);
and then on the saver.py file
import cgi
form = cgi.FieldStorage()
filename = form.getvalue("save_as")
print "Content-type: text/tsv"
print "Content-disposition: attachment; filename= %s" %filename
print
protlist = form.getvalue("protlist")
print protlist
I even tried to get cheeky and cheat-y and plug that in, and/or convert it
(and it may just be what you suggested last, but failed miserably to adapt
it)
Right now, with your first suggestion and as a quick test I've done:
var tsv2 = "cenas";
window.open(encodeURI('download_prots?filename='+file_name+'&text='+tsv2),
'_target');
only to be greeted with a
*invalid request*
So, probably it's best to try and adapt my old strategy... could I still
submit an "hand-crafted" form like that to a controller function? and how
would I "unpack" the variables? (cgi didn't seem to work here).
Well I'll keep at it and hopefully find a way, but appreciate any help
getting there :)
On Monday, October 14, 2013 8:18:15 PM UTC+1, Niphlod wrote:
>
> you can't get a "save as" dialog if the contents of the file are retrieved
> via ajax (browsers don't work like that). Open a new window pointing to the
> download url (it will soon close as there's no content to show, and the
> "save as" dialog will pop up).
>
> PS: you'd really want to use another system than the simple concatenation
> to build the url... if "content" in your case contains csv data, you will
> never be able to reach that url (because it won't be a valid url).
> As last resort, at least urlencode the content. But you'll then face the
> limitation of url size, and that's why usually those kind of things are
> made with intermediate temp files or (slightly more complicated in a new
> window) POST requests :P
>
> On Monday, October 14, 2013 6:15:51 PM UTC+2, Kariloy Markief wrote:
>>
>> Hello,
>>
>> I'm writing an app that at some point has a text string dynamically
>> generated in a view via javascript.
>> What I would like to do is download that text as a file.
>> For that purpose my attempted approach has been...:
>>
>> - on the same js function that generates the string I call:
>>
>> ajax('download_test?content='+content, [], '');
>>
>>
>> and on the controller I have:
>>
>>
>> def download_test():
>>
>> content = request.vars.content
>>
>> response.headers['Content-Type'] = 'text/csv'
>> attachment = 'attachment;filename=data.csv'
>> response.headers['Content-Disposition'] = attachment
>>
>> raise HTTP(200,str(content),
>> **{'Content-Type':'text/csv',
>> 'Content-Disposition':attachment + ';'})
>>
>>
>> I've tried several variations around this approach, and the only thing
>> that I can confirm is that the desired content is effectively passed from
>> the the view to the controller (and back to the view if I define a
>> container as the 3rd parameter of the ajax() function, but that's not what
>> I want).
>>
>> So the problem here seems to be be triggering the download.
>>
>> I've tried this on firefox (running my app), and also on chromium (via
>> minimal app just to reproduce this issue).
>>
>> I'm running web2py version 2.4.7
>>
>> Also the most similar reported issue I've found in this list was:
>> https://groups.google.com/forum/#!topic/web2py/GH2HVCGPMKo
>>
>> Because no answer was found then and I was unsure about this group's
>> policy of reviving old threads, I published mine as a new question. Hope
>> this is ok.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.