在我的应用程序(Yii)中,我有一个Ajax Submit Button,它触发了一个Excel文件(使用PHPExcel)的生成。 成功时我用一个隐藏的表单提交文件得到它下载:从request.responseText下载Excel文件
<div class="reporting">
<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'mydialog',
'options'=>array(
'title'=>'Warning!',
'autoOpen'=>false, //true,
'modal'=>'true',
'width'=>'auto',
'height'=>'auto',
'open' => 'js:function(event, ui) {
}',
'close' => 'js:function(event, ui) {
$(".ui-dialog:has(#mydialog)").empty().remove();
}',
)
));
?>
<table class="contentheader">
<tr>
<td>
<?php echo CHtml::DropDownList('dropDownId', $lastyear, $yearslist, array('options'=>array($lastyear=>array('selected'=>true)))); ?>
</td>
</tr>
</table>
<br />
<?php echo CHtml::ajaxSubmitButton('Form Ajax Submit Button',
CHtml::normalizeUrl(array('/planning/xlsAbsences')),
array(
'type'=>'POST',
'beforeSend' => "function(request) { console.log('beforeSend'); }",
'data'=>'js:$("#select-year-form").serialize()+"&year="+$("#dropDownId :selected").text()',
'success' => function(response, status, request){
$("#mydialog").dialog("close");
var disp = request.getResponseHeader("Content-Disposition");
if (disp && disp.search("attachment") != -1) {
var filename = disp.substring(disp.indexOf("filename=") + 10, disp.length-1);
var form = $("<form method=\"POST\" action=\"index.php?r=planning/dl\">");
form.append($("<input type=\"hidden\" name=\"content\" value=\"" + request.responseText + "\">"));
form.append($("<input type=\"hidden\" name=\"filename\" value=\"" + filename + "\">"));
$("body").append(form);
form.submit();
}
}
'complete' => "function(request) { console.log(request); }",
'error' => "function(data) { alert('erro'+data); }",
),
array('name' => 'run', 'class' => 'btn btn-success')
); ?>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');
echo CHtml::link(Yii::t('app','app.menu.reporting.planning.xlsabsence'), '#', array(
'onclick'=>'$("#mydialog").dialog("open"); return false;',
));
?>
</div>
问题是,生成的文件是不是有效的Excel文件。 我想Excelfile的内容是在request.responseText中。 我已经看到无法通过Excel打开的生成文件以UTF(不含BOM)编码。 如果我生成的文件没有Ajax Submit Button,那么文件就没问题。但它似乎是用ANSI编码的。 我使用Notpad ++来查看编码信息。当我将UTF(无BOM)转换为ANSI时,结果并不好。
我在这里错过了什么?
编辑: 我不知道我的描述是否不够清楚 - Excel文件不存在。在CJuiDialog中提交Ajax提交按钮时会生成Excel文件内容 - 例如,用户打开对话框并选择参数 - 根据用户选择的参数生成Excel文件。 由于该文件不存在,我看不到如何下载它。我发现唯一的解决方案是通过隐藏表单。
EDIT2: 我发表了整个视图代码(以上)
你实际提交检索到Excel中返回到服务器,迫使它下载?这似乎是一个巨大的开销。只需从Ajax调用中返回下载URL(它设置content-disposition标头等)并对此URL执行JavaScript重定向。 –
我编辑了我的帖子,使事情更清晰 - 你确定这可以做得更容易吗? –