weixin_33716154 2018-05-16 11:02 采纳率: 0%
浏览 132

如何下载Excel文件

I am working with the GUI where there is a button. On button click, an ajax call happens which returns a JSON to the backend in Django's view.py. On the basis of the JSON, I am creating an excel file and the path of this file is returned to the place where ajax call happened. I want that file to download on the click of the same button.

i.e, is it possible to do all these operations (from ajax call to download the excel file) with one click of that button?

edit

Path means the physical path of file system.

  • 写回答

1条回答 默认 最新

  • weixin_33743703 2018-05-16 11:39
    关注

    Yes it is possible to do so. Just make sure that the file is served from your STATIC_URL location ('static' folder by default). In your 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})
    

    In your ajax code,

    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.status == 200) {
          window.location = this.file_path;
        }
    };
    xhttp.open("GET", "/download", true);
    xhttp.send(); 
    
    评论

报告相同问题?