首页 > 技术文章 > Post文件下载

xiawg 2018-12-21 15:57 原文

/*===================下载文件
* options:{
* url:'', //下载地址
* data:{name:value}, //要发送的数据
* method:'post'
* }
*/
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) {
    var $input = $('<input type="hidden" name="' + key + '"/>');
    $input.val(config.data[key]);
    $form.append($input);
  }
  $iframe.append($form);
  $(document.body).append($iframe);
  $form[0].submit();
  $iframe.remove();
}

//文件下载按钮调用
function exports() {
  var items = $('#dgMain').datagrid('getChecked');
  var NO_Item = [];
  $.each(items, function (i, t) {
    NO_Item.push({ cDocNO: t.cDocNO, iItem: t.iItem })
  })

  DownLoadFile({
    url: '@Url.Action("DownLoad2", "_Excel")', //请求的url

    data: { NO_Item: JSON.stringify(NO_Item) }//要发送的数据
  })

}

 

注:Get请求Url长度有限制,当超出长度范围可用此方法代替Get请求

推荐阅读