【问题标题】:File not downloading with BLOB object in iphone chrome browser在 iphone chrome 浏览器中未使用 BLOB 对象下载文件
【发布时间】:2019-05-09 00:26:24
【问题描述】:

我正在尝试使用 ajax 调用从服务器 (jsp) 下载 pdf 文件,我从服务器获取 Base 64 格式的数据,然后将其转换为 ArrayBuffer,然后使用 blob 对象下载它,下面的代码适用于除了 iPhone 中的 chrome 之外的所有浏览器,即使在 iphone 的 Safari 中也可以正常工作,我不知道是什么问题,任何有关这方面的帮助将不胜感激

function hello(id)
    {
    //alert(id);

    //alert(id);
    var ln="en";
                $.ajax({
                  type:'post',
                 url:'ajaxurl',
                  data:{lang:ln,num_srno:id},
                  success:function(data){
                  //alert(data);

                /*  var bytes = new Uint8Array(data); // pass your byte response to this constructor

    var blob=new Blob([bytes], {type: "application/pdf"});// change resultByte to bytes

    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="myFileName.pdf";
    link.click();*/
    var sampleArr = base64ToArrayBuffer(data);
    saveByteArray("Sample Report", sampleArr);

                      }

                    });
    }

    function base64ToArrayBuffer(base64) {
        var binaryString = window.atob(base64);
        var binaryLen = binaryString.length;
        var bytes = new Uint8Array(binaryLen);
        for (var i = 0; i < binaryLen; i++) {
           var ascii = binaryString.charCodeAt(i);
           bytes[i] = ascii;
        }
        return bytes;
     }
    function saveByteArray(reportName, byte) {
        var blob = new Blob([byte], {type: "application/pdf"});
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        //link.href=window.webkitURL.createObjectURL(blob);
        //a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
        var fileName = reportName;
        link.download = fileName.substr(fileName.lastIndexOf('/') + 1);
        document.body.appendChild(link);
     link.click();
    document.body.removeChild(link);

    };

【问题讨论】:

  • 它在我的 Chrome 中运行。请记住,Chrome 具有自动下载功能。你能检查一下日志吗?
  • 它适用于 windows chrome ,osx( mac book ) chrome 但不适用于 i-phone chrome 浏览器。

标签: javascript jquery ios google-chrome blob


【解决方案1】:

iOS 的 Chrome 存在一些问题。就我而言,使用FileReader() 解决了这个问题:

var reader = new FileReader();
var out = new Blob([this.response], {type: 'application/pdf'});
reader.onload = function(e){
  window.location.href = reader.result;
}
reader.readAsDataURL(out);

【讨论】:

  • 它适用于 iphone chrome 但不适用于任何其他浏览器:(
  • 如何设置文件名?
【解决方案2】:

结合上面的Mose Answer,你可以检测操作系统类型并相应设置你的代码下载

function hello(id) {
  //alert(id);

  //alert(id);
  var ln = "en";
  $.ajax({
    type: "post",
    url: "ajaxurl",
    data: { lang: ln, num_srno: id },
    success: function(data) {
      //alert(data);

      /*  var bytes = new Uint8Array(data); // pass your byte response to this constructor

    var blob=new Blob([bytes], {type: "application/pdf"});// change resultByte to bytes

    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="myFileName.pdf";
    link.click();*/
      var sampleArr = base64ToArrayBuffer(data);
      saveByteArray("Sample Report", sampleArr);
    }
  });
}

function base64ToArrayBuffer(base64) {
  var binaryString = window.atob(base64);
  var binaryLen = binaryString.length;
  var bytes = new Uint8Array(binaryLen);
  for (var i = 0; i < binaryLen; i++) {
    var ascii = binaryString.charCodeAt(i);
    bytes[i] = ascii;
  }
  return bytes;
}

function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

  // Windows Phone must come first because its UA also contains "Android"
  if (/windows phone/i.test(userAgent)) {
    return "Windows Phone";
  }

  if (/android/i.test(userAgent)) {
    return "Android";
  }

  // iOS detection from: http://stackoverflow.com/a/9039885/177710
  if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
    return "iOS";
  }

  return "unknown";
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2019-07-19
    • 2018-03-20
    • 2018-05-21
    • 2013-11-12
    • 2019-09-03
    • 2014-10-27
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多