私は次の状況にあります:私は動的にファイルを作成する必要があります、ユーザーエージェントはこの方法で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() {
fileName = $( "#test")。find( "input")。val(); }
私はそれをこのように使用します:
......}).done(function(data) {
setTimeout("timeout_trigger()",7000);
});
そして、この2番目の解決策は常に機能するとは限らないので、この問題を解決する必要があります。既存のファイルが問題なくダウンロードされ、最近作成されたファイルがダウンロード時にエラーを表示するのはなぜですか?