我正在使用GUI,其中有一个按钮。单击按钮时,会发生ajax调用,将JSON返回给Django的view.py中的后端。在JSON的基础上,我创建了一个excel文件,这个文件的路径返回到发生ajax调用的地方。我想要那个文件在点击相同的按钮时下载。
也就是说,是否可以通过单击该按钮完成所有这些操作(从ajax调用到下载excel文件)?
edit
Path是指文件系统的物理路径。
发布于 2018-05-16 19:39:48
是的,这样做是可能的。只需确保该文件是从您的静态位置(默认情况下是‘STATIC_URL’文件夹)提供的。在你的views.py中,
def download(request):
# /static/ is your STATIC_URL in settings
file_path = "/static/xxxx.txt" # file path
return JsonResponse({"file_path": file_path})
在ajax代码中,
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.status == 200) {
window.location = this.file_path;
}
};
xhttp.open("GET", "/download", true);
xhttp.send();
https://stackoverflow.com/questions/50369168
复制相似问题