views:

71

answers:

2

Dear,

I'm a bit stuck with the following situation:

I'm using an ajax call to submit a form, i use the form values to do some 'query' in django. When this happened and i got the correct queryset it delivers me an HttP response in the form of a csv. I setted up the headers and all stuff needed to get the 'save file as' window... unfortunately, this doesn't happen because the ajax call puts the response inside a div.

This is the code:

if (vsc=='True'){
    ajax_loader();
 // add pdf, csv $.ajax (todo)
        $.ajax({ 
  type: "POST",
        url: "./test",
            dataType: 'text',
  success: test,
  data: {host: ho, log: lo, severity: se, datetimestart: dts, datetimeend: dte, enabled: ena, csv: vsc, pdf: fdp}

       });

So inside this ajax function a call the url ./test. Inside that url i do some object filtering and i make a http response object and send it back. On success i should like to get to 'save file as', but at this moment i have no clue how to do this. I succeeded in showing the response in a div on my html page ./test but the 'save file as' never shows up.

My django view:

def export_csv(request,queryset)
# do some filtering on queryset and create a csv
response = HttpResponse(''.join(csv_export), mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=customreportexport.csv'
return response

This is my view, everything should be normal, i think it's really that ajax call... on 'success' i have to do something so it popups as 'save file as', only i don't know how. I Already tried some iframe aswell but than i have to give the 'source' as an url and that's not possible as the ajax call does the post of the parameters etc... any help on this? Previously i knew there was in some language something called 'IsUpload=true', too bad it isn't in jquery ajax....

Hope to hear from you guys

+3  A: 

It sounds like this shouldn't be an Ajax request - if you just POST that form then the user will go to the csv url and be prompted to download the file.

jturnbull
it won't... i click submit on my form... my view see it needs to produce a csv, it returns an HTTPResponse object but it doesn't open 'save file as', how should you do it? This must be possible in jquery/ajax , it was possible in ExtJS... Could you give me an example how you should do it knowing that the POST values (parameters) could be different arrays as it are 'select boxes'...
Thomas
shreddies is right. Ajax is completely irrelevant and unnecessary here.
Daniel Roseman
it is not... my content is refrshing inside a page... i submit the same form only a checkbox confirms for a csv or not. When i return the response it will be returned to an ajax call so not to an external link... so it's not irrelevant. I made a solution for this, i just make a file temporary and do an ajax call with an iframe on the link of the temporary file... that's the only way this could work out in my case. Too bad jquery misses something like IsUpload = True like in ExtJS
Thomas
A: 

It's not, this is totally different, i know what he means... In my form there is checkbox called 'csv' , when it's clicked i do some ajax call which submit the form with the post values and it called up my view... in that view i say if csv: than blabla code and as last return Httpresponse... how can i show the user 'save file as' when i return an httpresponse object.... if he's right, well tell me how you should do it than...

thomas