如何将Django静态文件放入JavaScript数组中?

huangapple go评论75阅读模式
英文:

How can I put django static files in javascript array?

问题

var links = [
  '/static/uploads/fth/FthAgg.xlsx',
  '/static/uploads/fth/FthCon.xlsx'
];
{% 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>
  </body>
</html>

This wont work though. When I try to put https links it will work.

Here is my javascript file: https://pastebin.com/0sCTav8G

Here is my html template: https://pastebin.com/yvz2AL0M

英文:

I need to get static files from django to a "links" array...

var links = [
  &#39;/static/uploads/fth/FthAgg.xlsx&#39;,
  &#39;/static/uploads/fth/FthCon.xlsx&#39;
];

This is the html:

{% load static %}
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;{% static &#39;css/pages/fthpage.css&#39; %}&quot;&gt;
    &lt;script src=&quot;{% static &#39;js/downloads.js&#39; %}&quot;&gt;&lt;/script&gt;
    &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1 class=&quot;header_h1&quot;&gt;FTH Page&lt;/h1&gt;
      &lt;button onclick=&quot;downloadAll(window.links)&quot;&gt;Test me!&lt;/button&gt;

    &lt;/a&gt;
  &lt;/body&gt;
&lt;/html&gt;

This wont work though. When I try to put https links it will work.
Im doing this so that i can later download these files simultaniously with one button.

Here is my javascript file:
https://pastebin.com/0sCTav8G

Here is my html template:
https://pastebin.com/yvz2AL0M

答案1

得分: 0

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

    }    
}
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function downloadAll(links)
{
for (i = 0, len = links.length; i &lt; len; i++) {

	var link = links[i];
	var a = document.createElement(&quot;a&quot;);
	var file = &#39;file&#39;+ i + &#39;.xlsx&#39;;
	a.setAttribute(&#39;href&#39;, &#39;data:text/plain;charset=utf-8, &#39; 
                                         + link); 
                    a.setAttribute(&#39;download&#39;, file); 
                    a.click();
    
    }    

}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 21:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定