0

I have a function which is called via a user action in my template:

<a href="{% url 'channels:download_channel' channel.id %}" onclick="showLoader()">

download_channel is a function that merges audio files (into one file) and initiates a file for download. This can take some time and isn't instantaneous. So I show a spinner.

showLoader() is a javascript function that simply shows a spinner to let the user know the process/download is ongoing.

My problem is when the download is finished on the server side, I have no way of communicating with the template to hide the spinner

I've tried via ajax, but I guess you can't initiate downloads that way (unsafe). This was the ideal solution I was hoping would work.

    function downloadChannel() {
      showLoader()
      $.ajax({
              type:'GET',
              url:'{% url "channels:download_channel" topic.id %}',           
              success:function(json){
                console.log('got some success of heah')
                hideLoader();
                location.reload();
              },
              error : function(xhr,errmsg,err) {
              console.log(xhr.status + ": " + xhr.responseText);
              hideLoader();
              // provide a bit more info about the error to the console
          }
      });
    }

I've looked at django channels, but sockets seem like a big setup for such a simple problem.

I'm a bit stumped on how to properly approach this problem.

Any feedback is greatly appreciated

user3050491
  • 95
  • 4
  • 12
  • Is the 'unsafe' aspect the issue of passing the CSRF token with the request? That can be done, but you need to pass 'credentials' and 'headers' including 'X-CSRFToken' with the request. – Atcrank Dec 03 '19 at 21:56
  • It just doesn't result in a download. It runs the function and the process no problem, but nothing is extracted for the user. Unsafe is something I read, so can't back that up. – user3050491 Dec 03 '19 at 22:02
  • fwiw I had the CSRF token in the request on a previous try. The code above was just one iteration. – user3050491 Dec 03 '19 at 22:03
  • for future reference if anyone is in this bind -- I've had success with this thread [https://stackoverflow.com/questions/22724070/prompt-file-download-with-xmlhttprequest] which uses XMLHttpRequest – user3050491 Dec 03 '19 at 22:26

0 Answers0