<a href="" download=""> does not download when clicked

时间:2018-11-05 22:40:14

标签: javascript html flask web-applications

I'm building a web app in Python and Flask. This is the directory structure:

TopWebApp   
│
└───webApp
│   │   ui.py
│   │   back-end.py
│   │   result.txt
│   │
│   └───templates
│       │   index.html
│       │   ...
│   
└───venv
    | ...

In index.html, I have:

  <a href="C:\Users\...\webApp\result.txt" download>
    <button>click here</button>
  </a>

In chrome 70, when I click the "click here" button, nothing gets downloaded and if I open it in a new tab, the new tab will show "about::blank".

Since result.txt is saved under the same directory, I tried changing 'href=".result.txt"' and 'href="./result.txt"' but I get a Failed - No File when it downloads. Do you know why this happens?

1 个答案:

答案 0 :(得分:1)

作为一种安全措施,浏览器将阻止通过超链接链接到本地文件(例如polymorphic_identity驱动器上的文件)。该包括超链接和download attribute。如果要允许用户从服务器下载,则需要相对于服务器本身进行引用。可以使用诸如manager之类的绝对值来完成,尽管我建议使用相对链接,如:

    Col A    Col B   Col C
 0   a1     k1       v1  
 1   a1     k2       v2  
 2   a1     k3       v3  
 3   a2     k4       v4  
 4   a3     k5       v5  
 5   a3     k6       v6  

请注意,在此特定示例中,该文件是从与其链接的文件开始的一个文件夹,因此您需要使用# dict { "a1" : {"k1": "v1", k2: "v2", "k3": "v3"}, "a2" : {"k4": "v4"}, "a3" : {"k5": "v5", "k6": "v6"} } 浏览目录结构。

如果仅通过文件资源管理器直接打开文件(而不是dict = df.set_index('Col A').groupby('Col B').apply( lambda x: x.ColC.to_dict()).to_dict() ,则需要C:前缀,例如https://www.yourwebsite.com/yourfile.txt。请注意三个正斜杠:整个起点和正斜线。

相关问题