3

In the following code, if i change the file output format to output.csv the file gets automatically downloaded ,but if i change the format to output.txt the files gets displayed on the browser.How to auto download the output.txt file

Since csv file is getting downloaded what are the problem with txt file for the same code

   def downloadcsv(request):
        try:
            response = {}
            output = fq_name+"/"+ "output.txt"
            os.system(cmd)
            logger.debug(file_name)
            response.update({'status':0,'message': 'Success','file_name' : output})
        except Exception as e:
            response['message'] = "Exception while processing request"
            response['status'] = 2
            logger.exception(e)
        return HttpResponse(JsonResponse(response), content_type="application/json")

$.post("/reports/downloadcsv/", snddata,
        function callbackHandler(data, textstatus)
        {
            if (data.status == 0)
            {
                document.location.href = data.file_name;
                 //document.getElementById('my_iframe').src = data.file_name;
            }
            else if (data.status == 1 || data.status == 2)
            {
                $('#loading').hide();
                alert('Error while processing data');
            }
            $('#loading').hide();
         },
         "json"
         );
Rajeev
  • 44,985
  • 76
  • 186
  • 285
  • 2
    Possible duplicate of [Django download a file](http://stackoverflow.com/questions/36392510/django-download-a-file) – Sayse Sep 06 '16 at 10:26
  • duplicate: you need to send the Content-Disposition header – Anentropic Sep 06 '16 at 11:24
  • even if i don send the Content-Disposition header for csv file it is getting downloaded how is a txt file different now – Rajeev Sep 06 '16 at 11:38

2 Answers2

0

On a side note, why do you use "application/json" for a .txt file rather than .json?

It's hard to reliably control what different browsers do with a certain file or content type on the server side, as the browser behaves according to user's preferences: open in-browser, open save-as dialog, open in 3rd party program, handle with plugin, etc.

You could try the HTML5 download Attribute: http://www.w3schools.com/TAgs/att_a_download.asp

Other solutions proposed here: Force to open "Save As..." popup open at text link click for pdf in HTML

Community
  • 1
  • 1
djangonaut
  • 7,233
  • 5
  • 37
  • 52
0

You are trying to return a .txt, but it is set to return .json

Change

return HttpResponse(JsonResponse(response), content_type="application/json")

to:

return HttpResponse(JsonResponse(response), content_type="application/default")

I've tried with this code and it works:

from django.conf import settings
from django.http import HttpResponse, Http404

def download(self, request, path):
        file_path = os.path.join(settings.MEDIA_ROOT, path)
        if os.path.exists(file_path):
            with open(file_path, 'rb') as fh:
                response = HttpResponse(fh.read(), content_type="application/default")
                response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
                return response
        raise Http404