次のコードでは、ファイルの出力形式をoutput.csvに変更するとファイルが自動的にダウンロードされますが、出力形式をoutput.txtに変更するとファイルがbrowser.Howに表示されますcsvファイルを使用すると、「アプリケーション/ JSON」を使用しない理由サイドノート、上の同じコードdjangoから自動的に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"
);
可能な複製を(http://stackoverflow.com/questions/36392510/django-download-a-file) – Sayse
dupl icate:ダウンロードするCSVファイルのContent-Dispositionヘッダーを送信しなくてもContent-Dispositionヘッダー – Anentropic
を送信する必要があります。どのようにtxtファイルが異なるのですか – Rajeev