我需要将静态文件从 django 获取到“链接”数组...
var links = [
'/static/uploads/fth/FthAgg.xlsx',
'/static/uploads/fth/FthCon.xlsx'
];
这是 html:
{% load static %}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="{% static 'css/pages/fthpage.css' %}">
<script src="{% static 'js/downloads.js' %}"></script>
<title></title>
</head>
<body>
<h1 class="header_h1">FTH Page</h1>
<button onclick="downloadAll(window.links)">Test me!</button>
</a>
</body>
</html>
但这行不通。当我尝试添加 https 链接时,它会起作用。 我这样做是为了以后可以一键同时下载这些文件。
这是我的 JavaScript 文件: https://pastebin.com/0sCTav8G
这是我的 html 模板: https://pastebin.com/yvz2AL0M
最佳答案
function downloadAll(links)
{
for (i = 0, len = links.length; i < len; i++) {
var link = links[i];
var a = document.createElement("a");
var file = 'file'+ i + '.xlsx';
a.setAttribute('href', 'data:text/plain;charset=utf-8, '
+ link);
a.setAttribute('download', file);
a.click();
}
}
关于javascript - 如何将 django 静态文件放入 javascript 数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59579751/