【问题标题】:JQuery on Change Event Download FileJQuery on Change 事件下载文件
【发布时间】:2011-04-21 00:30:23
【问题描述】:

HTML 代码:

<span>Download Report as</span>
    <select id="comboFile">
        <option value="none">Select</option>
        <option value="http://somewhere/123.pdf">PDF</option>
        <option value="http://somewhere/123.csv">CSV</option>
    </select>

JQuery:

$(document).ready(function () {
       $("#comboFile").change(function () {
        if ($(this).val() != "none") GetFile($(this).val());
        $(this).val("none");
    });
});

function GetFile(fileURL){
///Code to be filled
//window.location.href = fileURL;///Does not seem to work for new window
}

我卡住了..如何启动文件下载..我需要填写获取文件功能的代码...

【问题讨论】:

  • 您的代码按给定的方式工作。设置window.location 应该可以工作:jsfiddle.net/XeAzy。注意设置window.location后写的JavaScript不会运行。
  • 定义“似乎不起作用”。
  • 对不起,我本来应该在窗口中说的。它应该是window.location.href = fileURL

标签: javascript jquery events onchange


【解决方案1】:
$(document).ready(function () {
       $("#comboFile").change(function () {
        if ($(this).val() != "none") GetFile($(this).val());
        $(this).val("none");
    });
});

function GetFile(fileURL){
///Code to be filled
//window.open(fileURL);
}

【讨论】:

  • 谢谢,这将在新窗口中打开文件。我要下载。
  • 啊,好吧。在这种情况下,您需要编写一些 PHP。本质上,您将该窗口的位置设置为myserver.com/download.php?file=myfile.pdf 之类的东西,然后您需要设置标题并使用PHP 的readfile() 函数。应该有很多更详细的帖子。
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
相关资源
最近更新 更多