在Chrome pdf查看器中打开pdf后无法下载pdf

时间:2018-11-19 14:48:26

标签: javascript pdf dynamics-crm

这是我的情况,我正在尝试获取Dynamics CRM上的自定义操作产生的pdf字符串,并让用户可以打开预览,然后,如果他愿意,可以从Chrome浏览器下载PDF文件Pdf Viewer,但如果我尝试下载它,则下载会因网络错误而被阻止。 这是打开预览的代码

function (params) 
     {
        // Success
        if (quest.toString() === "true") {
            alert("Documento Creato");
            this.location.reload(true);
        }
        else if (quest.toString() === "false") {
            var file = b64toBlob(params.Pdf, "application/pdf");
            if (window.navigator && window.navigator.msSaveOrOpenBlob) {

                window.navigator.msSaveOrOpenBlob(file);
                alert("Documento Creato");
            } else {
                var blobUrl = window.URL.createObjectURL(blobToFile(file,
                   params.PdfFilename));
                window.open(blobUrl);
            }
        }
        reloadPage();

     },

function b64toBlob(b64Data, contentType, sliceSize) {
   contentType = contentType || '';
   sliceSize = sliceSize || 512;

   var byteCharacters = atob(b64Data);
   var byteArrays = [];

   for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
       var slice = byteCharacters.slice(offset, offset + sliceSize);

       var byteNumbers = new Array(slice.length);
       for (var i = 0; i < slice.length; i++) {
           byteNumbers[i] = slice.charCodeAt(i);
       }

       var byteArray = new Uint8Array(byteNumbers);

       byteArrays.push(byteArray);
   }

   var blob = new Blob(byteArrays, { type: 'application/pdf' });
   return blob;
}

function blobToFile(theBlob, fileName) {
   //A Blob() is almost a File() - it's just missing the two properties below 
   //which we will add
   theBlob.lastModifiedDate = new Date();
   theBlob.name = fileName;
   return theBlob;
}  
`

感谢您的帮助。

0 个答案:

没有答案