0

I am sending an ajax request with a file to my backend that processes it. After processing it into another form (image into a compressed processed directory), I want to send that back to the user to down load that. Can someone provide an example view that will, when it receives the file, process the data the send a request that has the file. Also, please show me how to download that file in the javascript front end.

class send_text:
    def post(self, request):
        file = request.POST["file"]
        # Processing…………… file_to_send = <path to file>
        # show how to send the file here

Also the frond end:

$.ajax{
//sending the data
onSuccess: //show how to download the data here
}

Thank you in advance.

Kavi Vaidya
  • 119
  • 8
  • take a look at https://stackoverflow.com/questions/40662885/prompt-file-download for the download part to run in onSuccess and https://stackoverflow.com/questions/1156246/having-django-serve-downloadable-files for the serving the downloadable content. – AMG Oct 29 '18 at 00:49

1 Answers1

0

For anyone else, I have figured this out by using static files. Please use the tag with the download option. For an example:

<a href="{%get_static_prefix%}{{file_name}}/{{file_name}}.pdf" id="printer" download='{{file_name}}.pdf'></a>

Please note that if you wanna automatically download the file for the user, you make a function. Inside the function type this:

document.getElementById("<<name of <a> tag, here: printer>>").click()

Kavi Vaidya
  • 119
  • 8