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(); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示