post方式下载文件

来源:互联网 发布:vue.js主要是干什么用 编辑:程序博客网 时间:2024/04/29 19:17

post方式下载文件

一般在进行下载文件的时候使用的是get的方式,传入几个参数就行,但是如果是前台把大量的数据传给后台,需要使用post方式。而且经常使用的ajax的方式不能返回文件。
下面是一种解决的方案(需要JQuery):
var DownLoadFile = function (options) {    var config = $.extend(true, { method: 'post' }, options);    var $iframe = $('<iframe id="down-file-iframe" />');    var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');    $form.attr('action', config.url);    for (var key in config.data) {        $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');    }    $iframe.append($form);    $(document.body).append($iframe);    $form[0].submit();    $iframe.remove();};

调用的时候这样使用:
var data ={            filename:"testtest",            ExcelData:jsonString        };        DownLoadFile({url:BASE_URL + 'downloadFile/download',data:data});


0 0