log_path = settings.BASE_DIR + '/uploaded_files/logs/'
# log_file = user + '_' +datetime.datetime.now().__str__()
log_file = str(random.randint(1, 100)) + '_' + datetime.datetime.now().__str__()
log_file = log_path + '%s.txt' % log_file
download_error(LOG_FILE)
def download_error(log_file):
file_path = log_file
file_name = os.path.basename(file_path)
if os.path.exists(file_path):
print "DOWNLOAD ERROR FILE"
with open(file_path,'rb') as fh:
response = HttpResponse(content_type = "text/plain")
response['Content-Disposition'] = 'attachment; filename = %s' % file_name
return response
raise Http404
我没有下载文件???
答案 0 :(得分:0)
您似乎已正确打开该文件,但未能对其执行任何操作。
另外,您没有引用文件名。
要纠正这个问题,可能以下方法有效:
with open(file_path,'rb') as fh:
response = HttpResponse(fh.read(), content_type = "text/plain")
response['Content-Disposition'] = 'attachment; filename="%s"' % file_name
不要使用HttpResponse
,请查看FileResponse
,这应该更适合您的用例。