2011-04-21 50 views
1

HTML代码:JQuery的上更改事件下载文件

<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 
} 

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

+0

你的代码工作给出。设置'window.location'应该可以工作:http://jsfiddle.net/XeAzy/。请注意,设置'window.location'后写入的JavaScript将不会运行。 – 2011-04-21 00:36:58

+0

定义“似乎不起作用”。 – 2011-04-21 00:39:09

+0

对不起,我本来应该在一扇窗户里说。它应该是'window.location.href = fileURL' – 2011-04-21 00:41:04

回答

0
$(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); 
} 
+0

谢谢,这将打开新窗口中的文件。我想下载它。 – 2011-04-21 00:49:35

+0

啊,好吧。在这种情况下,你需要编写一些PHP。从本质上讲,你该窗口的位置设置成类似http://myserver.com/download.php?file=myfile.pdf 然后,你需要设置页眉和使用PHP的ReadFile的()函数。 应该有很多更多的细节职位。 – 2011-04-21 01:16:27