0

My goal is to download a csv file from dynamic content handled by Angularjs. The file is constructed server side by Django.

It is easy to get a file with Django. We only need a view to return an HttpResponse (doc). On the client side, I can then get it by changing the browser's url with $window.location.href.

If I have a few parameters, I can give them as query parameters to window.location with search (Mozilla doc).

However I must pass a large list of data to the server, so I can't use query parameters. (I'm limited by the max length of urls).

With AngularJS' $http, we can set a lot more config to a GET, including data parameters: doc. This data is "to be sent as the request message data", it is not encoded to the url. I'd like to use that.

So, my pb would be solved if I could set this data with the $window.location.href call. Is it possible ?

If I fire a GET with Angular's $http, Django's HttpResponse isn't interpreted as a response, it is intercepted as data into the success clause. How can I make use of it ?

Do you see another strategy to create and download dynamic files ?

edit: I went with this for a csv file.

other strategies ideas:

Many thanks !

Ehvince
  • 17,274
  • 7
  • 58
  • 79

1 Answers1

1

If you want to request data from a server based on parameters you should send them as GET query params, however if you send a large amount of parameters you should send them in the GET request body (the $http data field). Angular is fitted to handle JSON, so if you have to handle other data (e.g. XML, CSV) you will have to transform it to JSON. Angular provides a transformRequest and a transformResponse function (see here) in the $http config object to do exactly that (you have to write the logic by yourself though).

$window.location.href is not suitable as it redirects the browser window to the specified location (usually expecting some html), which would return the data but you'd have no way to interact with it in a JS/Angular context.

I strongly suggest you use $http.get and transform your data as required. Maybe you find a library that handles the conversion for you.


kreck
  • 68
  • 4
  • Thanks, it's a clear indication. In your second paragraph, you say that with `$window…href` I'd have no way to interact with its returned content in a JS context. That doesn't seem a pb to me: I just need a context before the `href` redirection and to download the file after it. – Ehvince Mar 10 '16 at 16:11