美文网首页
兼容ie的导入导出

兼容ie的导入导出

作者: nzjcnjzx | 来源:发表于2019-07-23 09:50 被阅读0次
     /**导入文件 */
      function importFile(options) {
        if (!$("#input-file").val()) {
          request.myAlert("請選擇檔案!");
          return;
        }
        var params = {
          url: options.url,
          data: {
            type: oralCurrentIndex,
            centerNo: options.centerNo,
            token: token
          },
          success: function (result) {
            console.log('result')
            // 判断是否是ie9
            if (isIeBrowser()) {
              try {
                var res = JSON.parse(result);
              } catch (e) {
                if(e){
                  request.myAlert('服務器異常,請聯系管理員!');
                }
              }
              if(res&&$.isEmptyObject(res)){
                $(".mask").hide();
                IndexUtil.refreshTable();
              } else if(res&&res.errorMsg){
                request.myAlert(res.errorMsg);
              }
            // 其他浏览器
            } else {
              $(".mask").hide();
              IndexUtil.refreshTable();
            }
          },
          error: function (err) {
            try {
              request.myAlert(JSON.parse(err.responseText).errorMsg);
            } catch (error) {
              if(error){
                request.myAlert('服務器異常,請聯系管理員!');
              }
            }
          },
          resetForm: true,
        };
        $("#fileForm").ajaxSubmit(params);
      }
    
    
      function isIeBrowser() {
        return navigator.appName == "Microsoft Internet Explorer" &&
          parseInt(
            navigator.appVersion
            .split(";")[1]
            .replace(/[ ]/g, "")
            .replace("MSIE", "")
          ) <= 9
      }
    导出
    function exportF() {
          var url = /word/exportFilePersonal' // 请求的url + id
          var xhr = new XMLHttpRequest();
          // var filename = 'file' // 文件名称
          xhr.open("POST", url, true)
          xhr.responseType = "blob";
          // xhr.setRequestHeader("client_type", "DESKTOP_WEB");
          xhr.setRequestHeader("token", token); // 自定义请求头
          xhr.onload = function () {
            if (this.status == 200) {
              var blob = this.response;
              console.log(this)
              console.log(blob)
              // var fileName = filename;
              if (document.documentMode) { // IE浏览器下
                navigator.msSaveOrOpenBlob(blob, '1.xlsx');
                console.log('ieee')
              } else {
                console.log('不是ie')
                var link = document.createElement("a");
                link.href = window.URL.createObjectURL(blob);
                link.download = '1.xlsx';
                link.click();
                window.URL.revokeObjectURL(link.href);
              }
            }
          };
          var data = {
            "centerNo": "NO-1",
            "type": 1,
            "userId": "string"
          }
          xhr.send(JSON.stringify(data))
        }
      
        function exportFile() {
          var d = {
            "centerNo": "NO-1",
            "type": 1,
            "userId": "string"
          }
          var url = 'http://10.50.130.35:8002/word/exportFilePersonal' // 请求的url + id\
          console.log('开始请求')
          $.ajax({
            url: url,
            type: 'POST',
            headers: {
              token: token
            },
            data: JSON.stringify(d),
            contentType: "application/json;charset=utf-8",
            dataType: 'binary',
            responseType: 'blob',
            success: function (blob) {
              console.log(11111111111)
              console.log(blob)
              // const blob = new Blob([data], {
              //   type: 'application/octet-stream'
              // });
              // 兼容IE浏览器
              if (window.navigator.msSaveOrOpenBlob) {
                window.navigator.msSaveOrOpenBlob(blob, fileName);
              } else {
                // 火狐、chrome浏览器通过a标签进行下载
                const a = document.createElement('a');
                a.id = 'exppub';
                // 保证pdf和html文件不在浏览器中打开,而是下载
                a.download = fileName;
                a.href = URL.createObjectURL(blob);
                document.body.appendChild(a);
                const aLink = document.getElementById('exppub');
                aLink.click();
                aLink.parentNode.removeChild(a);
              }
            },
            error: function (err) {
              console.log(err)
              console.log(err.responseText)
              var data = err.responseText;
              var blob = new Blob([data], {
                type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
              });
              console.log(blob)
              if (window.navigator.msSaveOrOpenBlob) { // IE浏览器下
                navigator.msSaveOrOpenBlob(blob, '1.xlsx');
                console.log('ieee')
              } else {
                console.log('不是ie')
                var link = document.createElement("a");
                link.href = window.URL.createObjectURL(blob);
                link.download = '1.xlsx';
                link.click();
                window.URL.revokeObjectURL(link.href);
              }
            }
          });
        }
    

    相关文章

      网友评论

          本文标题:兼容ie的导入导出

          本文链接:https://www.haomeiwen.com/subject/ecwnlctx.html