【问题标题】:Downloading a file from Javascript onclick function in python从python中的Javascript onclick函数下载文件
【发布时间】:2012-04-24 14:14:26
【问题描述】:

假设我在一个页面上有一个名为“点击下载”的锚标签,点击该标签会下载一个 csv 文件

HTML 代码是

<span id="tag_id">
    <a class="classA" onclick="calling_an_excel_function()" href="#"> Click here to download all records.</a>
</span> 

其实是调用Javascript的Onclick函数,生成一个csv文件,onclick函数的代码是

function calling_an_excel_function() {
    var exampleA = document.getElementById('exampleA.value');
    var exampleB = document.getElementById('exampleB.value');
    var exampleC = document.getElementById('exampleC.value');
    var exampleD = document.getElementById('exampleD.value');
    window.open('indices_main_excel.aspx?ind=' + exampleA.value + '&fromDate=' + exampleB.value + '&toDate=' + examplec.value + '&DMY=' + exampleD.value, '_blank',
'toolbar=yes,location=no, directories=no, status=no, menubar=yes,scrollbars=yes, resizable=yes ,copyhistory=no, width=800, height=600');
} 

我可以预期,在 onclick 函数中,window.open() 方法会生成一个新文件,并给出一些值。 现在我无法从上面的 onclick 函数中获取 csv 文件下载 url 我可以知道如何获取 csv 文件下载链接

【问题讨论】:

    标签: javascript python html


    【解决方案1】:

    (只是猜测)

    但你可能想要

    document.getElementById('exampleA');
    

    而不是

    document.getElementById('exampleA.value');
    

    【讨论】:

      【解决方案2】:

      您不需要使用 Window.open。将 href 更改为等于文件路径(是的,它来自您的服务器代码)或使用 window.location。确保设置了 content-disposition 标头。

      我不知道如何在 python 中设置标头,但您需要设置如下所示的标头:

      Content-Disposition:附件;文件名="文件名.csv"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2012-10-03
        相关资源
        最近更新 更多