1

Part which is working fine :

I have made a <form> whose submit call makes an ajax request.Code for the same :

 $.ajax({

            type: 'POST',
            url: '/uploaded_proto_file/',
            data: formdata,
            processData: false,
            contentType: false,

            success: function (data) {
                console.log("in success")
                console.log("data is --->"+data)
                return;

            },
            error: function (data) {
                console.log("in error")

                return;
            }

        });

I am able to receive the call for the same on the function below. Now i need to send a file (which is in my document structure) that needs to auto downloaded in my browser. I execute this function based on this answer

def uploaded_proto_file(request):
   with open(
            '/Users/metal/Documents/test_pb2.py','rb') as text_file:
        response = HttpResponse(FileWrapper(text_file.getvalue()), content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename=test_pb2.py'
        return response

The above code wasn't working fine, so I debugged it and found this error on the filewrapper statement

snapshot of error

Now I changed the solution a bit but the file sent back in response is not getting auto downloaded , it is getting printed in the console (as my success block in ajax function has console.log(data))
Changed solution :

with open(
            '/Users/metal/Documents/test_pb2.py','rb') as text_file:
        response = HttpResponse(text_file, content_type='application/zip')
        response['Content-Disposition'] = 'attachment; filename=test_pb2.py'
        return response

As you can see above I removed FileWrapper and was atleast able to send the file back to ajax request. But the problem still persists as the file is not getting auto downloaded.

P.S. : I have also tried opening the file in different modes like 'r','rb'.

Any help would be appreciated !

Metal
  • 35
  • 5

1 Answers1

0

You can't return a file to an ajax request and expect it to be downloaded. What you need is to separate into a few steps:

  • Your ajax request triggers the file creation process
  • Django puts that file somewhere where it can be downloaded and returns the URL to the file as ajax response
  • Your js response handler can now do two things:
    1. Generate a button in the HTML for the user to click and download the file (using the URL from the response)
    2. Create the button and auto-click it (call .click() on it) and then delete it again. A bit like in this example
dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • I understood the second part but is there a way I can do it without the downloadable url , I dont want to upload the file somewhere else and then pass the url when I already have the file content in my success block – Metal Apr 14 '18 at 17:10
  • Actually, if you look at the example linked in my response, you can use a data URI in your download button, so maybe try that. – dirkgroten Apr 14 '18 at 17:16