我建议用隐藏的iframe代替ajax请求,然后当服务器返回上述文件时,它将自动要求用户下载它。
//name of iframevar strName = ("uploader" + (new Date()).getTime());// the iframevar jframe = $( "<iframe name="" + strName + "" src="about:blank" />" ).css( "display", "none" );jframe.load(function( objEvent ){ // at this point the user should have been asked to download a file. // Remove the iframe from the document. // Because FireFox has some issues with // "Infinite thinking", let's put a small // delay on the frame removal. setTimeout(function(){ jframe.remove(); },100);});var form = $('<form>').attr( "action", "upload_act.cfm" ) .attr( "method", "post" ) .attr( "enctype", "multipart/form-data" ) .attr( "encoding", "multipart/form-data" ) .attr( "target", strName );form.append('<input type="hidden" name="somename">').val("someval");$( "body:first" ).append( jframe, form );(以上代码原始改编自http://www.bennadel.com/blog/1244-ColdFusion-jQuery-And-AJAX-File-
Upload-Demo.htm)
另一种选择是将其分为两个步骤。步骤1生成文件并返回url,步骤2用户单击下载(这将是指向该url的锚标记)。



