日期:2014-05-18  浏览次数:20714 次

求救:如何用jS实现从服务器端下载TXT文件到本地?
现在在前端JSP页面上有一个按钮,当你当击后,会调后台的一个服务,这个服务经过一系列处理后,会自动生成一个TXT文件,存放在如:http://168.0.0.1:8008/work/print/down目录下,并给前台返URL地址过来。
我现在想通过这个URL地址,把文件下载到地机,我用window.open("URL");时,会把这个文件直接打开,我现在想做成像网上下载文件一下,点按钮后,让你下载到本机,而不是直接打开,各位大哥前辈有什么办法可以实现吗?

<input type="button" name="out_excel" value="导 出"onclick="FileTransteller()">//这是JSP页面中一部分
//这是写的一个JS函数,ALLSubmit这个方法会去调EduceCpcMessageFile这个服务,
function FileTransteller(){
var input = new Array();
input['user']=document.all.user.value;

var res = ALLSubmit("EduceCpcMessageFile",input);//EduceCpcMessageFile后台服务类,input为JSP提交台参数
if(!res){
return null;
  }
var fileServicePath = res[2]['fileServicePath'];//若成功刚返TXT的URL地址回来
alert("fileServicePath = " + fileServicePath);
alert("sys_tellerRequestPath = " + get_control_value("sys_tellerRequestPath"));
window.open(fileServicePath);//这个方法就直接把这个TXT以浏览器的方式打开了
}
fileServicePath 这个URL是可以返回来的,我如何去实现弹出个下载框,然后下载到本机呢?求各位大哥、前辈帮忙...


------解决方案--------------------
JScript code

function FileTransteller(){ 
var input = new Array(); 
input['user']=document.all.user.value; 

var res = ALLSubmit("EduceCpcMessageFile",input);//EduceCpcMessageFile后台服务类,input为JSP提交台参数 
if(!res){ 
  return null; 
  } 
var fileServicePath = res[2]['fileServicePath'];//若成功刚返TXT的URL地址回来 
alert("fileServicePath = " + fileServicePath); 
alert("sys_tellerRequestPath = " + get_control_value("sys_tellerRequestPath")); 
var test =window.open(fileServicePath);//这个方法就直接把这个TXT以浏览器的方式打开了 
test.document.execCommand("SaveAs");   
test.close();   
}