post方式实现下载文件、打开新页面(ie8可用)
** 项目迁移到了云环境,带有参数的a标签,点击一次下载有时会下载两个文件。所以改为post方式提交下载需要的参数**
代码中append()方法行顺序不要改变,会影响ie8不生效
var DownLoadFile = function (options) {
var config ={ method: 'post' };
var $iframe = $('<iframe id="down-file-iframe" />');
var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
$(document.body).append($iframe);
$iframe.append($form);
$form.attr('action', options.url);
for (var key in options.data) {
$form.append('<input type="hidden" name="' + key + '" value="' + options.data[key] + '" />');
}
$form[0].submit();
$iframe.remove();
};
//调用方法实现下载。
DownLoadFile({
url:BASE_URL + 'Download/testDownLoad',
data:Exceldata
})
上面js会打开一个新窗口/页面,下载文件或者展示内容。