ดาวน์โหลดไฟล์ txt อัตโนมัติจาก django

ในโค้ดต่อไปนี้ ถ้าฉันเปลี่ยนรูปแบบไฟล์เอาต์พุตเป็น output.csv ไฟล์จะถูกดาวน์โหลดโดยอัตโนมัติ แต่หากฉันเปลี่ยนรูปแบบเป็น output.txt ไฟล์ก็จะแสดงบนเบราว์เซอร์ วิธีดาวน์โหลดไฟล์ output.txt โดยอัตโนมัติ

เนื่องจากไฟล์ csv กำลังได้รับการดาวน์โหลด อะไรคือปัญหาของไฟล์ txt สำหรับโค้ดเดียวกัน

   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"
         );

person Rajeev    schedule 06.09.2016    source แหล่งที่มา
comment
อาจซ้ำกับ Django ดาวน์โหลดไฟล์   -  person Sayse    schedule 06.09.2016
comment
ซ้ำกัน: คุณต้องส่งส่วนหัว Content-Disposition   -  person Anentropic    schedule 06.09.2016
comment
แม้ว่าฉันจะไม่ส่งส่วนหัว Content-Disposition สำหรับไฟล์ csv มันกำลังถูกดาวน์โหลด ไฟล์ txt แตกต่างอย่างไรในตอนนี้   -  person Rajeev    schedule 06.09.2016


คำตอบ (2)


เหตุใดคุณจึงใช้ "application/json" สำหรับไฟล์ .txt แทนที่จะเป็น .json

เป็นการยากที่จะควบคุมสิ่งที่เบราว์เซอร์ต่างๆ ทำกับไฟล์หรือเนื้อหาบางประเภทบนฝั่งเซิร์ฟเวอร์ได้อย่างน่าเชื่อถือ เนื่องจากเบราว์เซอร์ทำงานตามความต้องการของผู้ใช้: เปิดในเบราว์เซอร์ เปิดกล่องโต้ตอบบันทึกเป็น เปิดในโปรแกรมบุคคลที่สาม จัดการด้วยปลั๊กอิน ฯลฯ

คุณสามารถลองใช้แอตทริบิวต์การดาวน์โหลด HTML5: http://www.w3schools.com/TAgs/att_a_download.asp

วิธีแก้ปัญหาอื่น ๆ ที่เสนอที่นี่: บังคับให้เปิดบันทึกเป็น... ป๊อปอัปเปิดที่ลิงก์ข้อความ คลิกเพื่อดู pdf ในรูปแบบ HTML

person djangonaut    schedule 06.09.2016

คุณกำลังพยายามส่งคืน .txt แต่ถูกตั้งค่าให้ส่งคืน .json

เปลี่ยน

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

to:

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

ฉันได้ลองใช้รหัสนี้แล้วและใช้งานได้:

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
person Natan Almeida de Lima    schedule 09.02.2021