简体   繁体   English

请求完成时,如何在 React js 中访问 api 调用的响应数据?

[英]How can I access response data of api call in react js while request is completing?

Actually I am getting pdf bytes from api call and I want to show pdf data as content is being downloaded but I am not able to access response data before the request is completed and for large pdf files I have to wait for few seconds for request to get completed and display it.实际上,我从 api 调用中获得了 pdf 字节,我想在下载内容时显示 pdf 数据,但在请求完成之前我无法访问响应数据,对于大型 pdf 文件,我必须等待几秒钟才能请求完成并显示它。

For this question there are 4 generic types of "PDF"对于这个问题,有 4 种通用类型的“PDF”

  1. A Text reprex of a PDF (thus not a PDF) often served as a base64 text data uri stream that is downloaded and converted in the browser into a true PDF. It is thus ideal for very small files streams but the delay is that it is must be inflated to transmit, then 100% received and converted (re-compressed) in the device before the same behaviour as a true PDF thus the slowest to transmit and slowest to view. PDF(因此不是 PDF)的文本表示通常作为 base64 文本数据 uri stream 在浏览器中下载并转换为真正的 PDF。因此它非常适合非常小的文件流,但延迟是必须膨胀才能传输,然后在与真实 PDF 相同的行为之前在设备中 100% 接收和转换(重新压缩),因此传输速度最慢,查看速度最慢。

  2. A standard PDF which has a sting in its tail that it cannot be viewed until the full file has been downloaded and the trailer xref table can point to where the myriad of bits of page 1 can be found thus takes time to decompress and decompile the screen stream graphics.一个标准的 PDF 尾部有一个刺,在下载完整文件之前无法查看它,并且尾部外部参照表可以指向可以找到第 1 页的无数位的位置,因此需要时间来解压缩和反编译屏幕stream 图形。 Thus streaming is of no value for early viewing, only faster transportation than base64.因此,流式传输对于早期观看没有任何价值,只是比 base64 传输更快。

  3. A Liniarized PDF, this has been designed to show page 1 on arrival, since it keeps part of the tail at the head of the file (hence the term Web optimised) However it has the same need to download fully into the device before ALL the pages can be viewed. Liniarized PDF,它被设计为在到达时显示第 1 页,因为它将尾部的一部分保留在文件的头部(因此术语 Web 被优化)但是它同样需要在所有文件之前完全下载到设备中可以查看页面。 Here described by PDFTron https://www.pdftron.com/blog/mobile/streaming-a-pdf-from-the-web/此处由 PDFTron 描述https://www.pdftron.com/blog/mobile/streaming-a-pdf-from-the-web/

  4. A specialist streaming transmission where the pdf is chunked and sent to a specialist viewer that re-assembles the parts as required.一种专业的流媒体传输,其中 pdf 被分块并发送给专业观众,由专业观众根据需要重新组装各个部分。 Thus is a commercial solution.因此是一种商业解决方案。 One example I can think of is PDFTron.我能想到的一个例子是 PDFTron。 Not strictly your average PDF file method of transmission, so unlikely to be seen in general public PDF viewers.不是严格意义上的平均 PDF 文件传输方式,因此不太可能在公众 PDF 观众中看到。

Thus in answer to your question you need to convert your PDFs to be "Linearized"因此,在回答您的问题时,您需要将 PDF 转换为“线性化”

You probably want to stream the data using a streams API .您可能希望 stream 使用流 API的数据。

Here is an example of a png being loaded (in "chunks"): https://mdn.github.io/dom-examples/streams/png-transform-stream/这是一个正在加载的 png 的示例(以“块”形式): https://mdn.github.io/dom-examples/streams/png-transform-stream/

(You'll need to disable caching and throttle your inte.net speed in dev tools to see it load in pieces). (您需要在开发工具中禁用缓存并限制您的 inte.net 速度才能看到它分段加载)。

I believe PDF.js supports streaming我相信PDF.js支持流式传输

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本机反应中访问 api 调用响应中的特定数据 - how to access particular data in api call response in react native React.JS,如何使用来自第二个 API 调用的数据编辑第一个 API 调用的响应? - React.JS, how to edit the response of a first API call with data from a second API call? 我如何使用带有 props react js 的 API 调用来渲染组件 - how can i render a component with an API call with props react js React.js 尝试访问 API 提取的响应,该响应有效并在我的 /api 中返回数据,但我无法在我的 /src/ 中访问 - React.js Trying to access the response of an API fetch that works and returns data in my /api but I cannot access in my /src/ 如何在 React 中操作来自 api 调用的数据? - How can i manipulate data from api call in React? 进入api时调用api响应 - call api response on getting in react js 无法访问 React js 中的 Api 响应 - cant access Api response in react js React js对API请求的第二次调用 - React js Second call to API request 如何使用 React js 在表中呈现来自 API 响应的数据 - How to render data from a API response in a table using react js 如何根据另一个请求的响应向 API 发出请求? 不能在回调中调用 React Hook “useSwr” - How can I make a request to an API based on the response from another request? React Hook “useSwr” cannot be called inside a callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM