在 Javascript 中将 Azure 文件服务中的二进制或应用程序/八位字节流内容转换为 PDF

问题描述

我目前正在开发一个 Ionic/Cordova 应用程序,该应用程序通过 File Service REST API 浏览 Azure 文件服务 - 我能够成功地使用 { {3}} 带回包含其内容的 PDF 文件。这是响应 JSON(注意:data 字段已被剪断):

{
   status:200,url:"https://fileshare.file.core.windows.net/fileshare/Hello%20world.pdf",data:"...ù÷Ìûî}°»ÀÂr¼d@...",headers: {
      accept-ranges:"bytes",access-control-allow-origin:"*",content-length:"182162",content-type:"application/octet-stream",date:"Fri,21 May 2021 13:49:48 GMT",etag:""435fsdsdfds4"",last-modified:"Fri,21 May 2021 13:49:04 GMT",server:"Windows-Azure-File/1.0 Microsoft-HTTPAPI/2.0",x-android-received-millis:"1621604989117",x-android-response-source:"NETWORK 200",x-android-selected-protocol:"http/1.1",x-android-sent-millis:"1621604989033",x-ms-file-attributes:"Archive",x-ms-file-change-time:"2021-05-21T13:49:03.8588220Z",x-ms-file-creation-time:"2021-05-21T13:49:03.8588220Z",x-ms-file-id:"43543fsddf45634"
 }

}

这里返回的数据 Content-Typeapplication/octet-stream。从这里开始,我想将 data 的值转换为 PDF 并将其保存在本地以供查看。`

我一直在尝试使用 Get File endpoint文件直接写入本地存储:

            let newBlob = new Blob([result.data],{ type: "application/octet-stream" })

            return fromPromise(this.file.writeFile(this.file.externalDataDirectory,"test.pdf",newBlob,{ replace: true }))

运行此代码确实会在设备上创建一个 PDF,但是当我尝试打开该 PDF 时,出现错误

无法显示 PDF(test.pdf 格式无效)

我很可能遗漏了几个关键步骤 - 任何指导将不胜感激。

谢谢, 本格拉

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)