我需要一些帮助或建议。在django应用程序的模板中,我有iframe小部件,其中用户选择日期并通过我在js中设置的url地址加载xls文件。如果不存在具有所选日期的文件,我想显示有关它的消息。出于某种原因,在这种情况下iframe创建空的xls文件。如何解决?
JS:
var downloadSideBarFiles = {
main_url: "https://example.com",
file_types: {
2: "/shares/rpfile/",
1: "/shares/waprfile/"
},
init: function() {
var e = $(".regulations__download");
this.clickMethod(this, e)
},
clickMethod: function(e, a) {
a.find(".regulation__download-link").click(function(t) {
t.preventDefault();
var r = a.find("input[type=radio]:checked"),
o = a.find("input[type=text]");
0 == r.length ? e.errorMethod("No file type selected") : 0 == o.val().length ? e.errorMethod("No date selected") : (type = e.file_types[r.val()], date = o.val().split("/").join("."), uri = e.main_url + type + date, e.downLoadMethod(e, uri))
})
},
errorMethod: function(e) {
alert(e)
},
downLoadMethod: function(e, a) {
$.fileDownload(a).fail(function() {
e.errorMethod("File is not exist")
})
}
};
views.py:
def warpfile_date(request, date):
if urllib.urlopen(filepath+ 'wapr' + str(date) + '.xls').code == 200:
file = urllib.urlopen(filepath)
response = HttpResponse(file, content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=' + 'wapr' + '.xls'
else:
response = HttpResponse(status=404)
return response