我有以下情况:我必须动态创建文件,用户代理以这种方式通过ajax向服务器发送数据:
$("#generateButton").live("click",function(){
dateLimite = $("#dateLimite").val();
$.ajax({
type : "POST",
url : "../generateFile.do",
data : { fileName: "demandeExplication",
nbrParam: "3",
param1:"<%=agent.getPrenomAgentArabe()+" "+agent.getNomAgentArabe()%>",
param2:"<%=descriptionActe%>",
param3:dateLimite,
},
dataType: "html",
}).done(function(data) {
$("#test").empty().append(data);
fileName = $("#test").find("input").val();
$.fileDownload('http://localhost:8080/gestionRH/fiches/temp/'+fileName);
});
});
服务器使用动态创建文件的操作处理此数据:
public class GenerateFile extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
String fileName = request.getParameter("fileName");
Integer nbrParam = Integer.parseInt(request.getParameter("nbrParam"));
String[] valueParam = new String[nbrParam+1];
for(int i =1;i<=nbrParam;i++)
{ System.out.println(request.getParameter("param"+i));
valueParam[i]=request.getParameter("param"+i);
}
FileInputStream in = new FileInputStream("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\"+fileName+".doc");
POIFSFileSystem fs = new POIFSFileSystem(in);
HWPFDocument doc = new HWPFDocument(fs);
Range r = doc.getRange();
for(int i=1;i<=nbrParam;i++)
{ System.out.println("<param"+i+">");
System.out.println(valueParam[i]);
r.replaceText("<param"+i+">", valueParam[i]);
}
File frr = new File("C:\\Users\\free\\Desktop\\myworkspace\\gestionRH\\WebRoot\\fiches\\temp");
File temp = File.createTempFile("monfile",".doc",frr);
FileOutputStream out = new FileOutputStream(temp);
doc.write(out);
out.close();
in.close();
request.setAttribute("fileName", temp.getName());
return mapping.findForward("fileName");
}
}
我使用这个插件进行下载:http: //johnculviner.com/category/jQuery-File-Download.aspx
我收到错误下载!我没有得到现有文件,或者当我在一段时间后使用此代码触发下载时:
function timeout_trigger() {
文件名 = $("#test").find("input").val(); }
我以这种方式使用它:
......}).done(function(data) {
setTimeout("timeout_trigger()",7000);
});
而第二个解决方案并不总是有效,所以我必须解决这个问题。为什么下载已经存在的文件没有问题并且最近创建的下载时显示错误?