Onlyoffice binary stream preview

Ask a how-to question

I want to preview using a PDF binary stream, but there is an error message that says’ download failed ', and there is also a request 403

Request URL: http://x.x.x.x:18760/downloadfile/document-1856194310425788418-1731395483584
Request Method:POST
Status Code:403 Forbidden
Request Payload:{url: “blob:http://localhost:3100/90684a5a-dcdb-46aa-994c-ce153b02b215”}

my code:

async function previewFile(row) {
    const res = await queryFileStreamWaterMarkApi({ id: row.id })
   // res is binary stream
    // const blob = new Blob([res], { type: 'image/png' })
    // const src = window.URL.createObjectURL(blob)
    let url = window.URL.createObjectURL(res)
    fileInfo.value = {
      ...row,
      filePath: url,
    }
  }

If we change the filePath to http://xjrbmgl.ts.cn/newspaper/2024-11-10/19/52-2792358.pdf It can be displayed

  async function previewFile(row) {
    const response = await queryFileStreamWaterMarkApi({ id: row.id })
    // 创建一个指向Blob对象的URL
    const url = URL.createObjectURL(response)
    fileInfo.value = {
      ...row,
      filePath: url,
    }

    // 创建一个隐藏的<a>标签用于下载
    const a = document.createElement('a')
    a.href = url
    a.download = 123456789 + '.pdf'

    // 触发点击事件以下载文件
    document.body.appendChild(a)
    a.click()
  }

Can be downloaded locally and previewed,But onlyoffice can’t show it

Hello @orson

Unfortunately, Document Server can handle file only via direct URL to it, binary steam of the file is not supported.

If your custom function returns a file in a downloadable form, i.e. same way as direct URL, then you should take a look at logs for the reason why resulting link cannot be accessed with 403.