简体   繁体   English

Blazor 服务器 - websocket 连接失败 1006 - PDF 查看器(文件大小为 13mb 到 20mb)

[英]Blazor Server - websocket connection to failed 1006 - PDF Viewer (with file size 13mb to 20mb)

Good Day everyone今天是个好日子

I'm having a problem when using pdf viewer using Blazor Server (ASP.NET Core), the error occurs when we deployed it on the staging environment, the file type is pdf and the usual file size is 13 mb to 20 mb.我在使用 Blazor 服务器(ASP.NET Core)使用 pdf 查看器时遇到问题,当我们将其部署在暂存环境中时出现错误,文件类型为 Z437175BA4191210EE009 至 20,通常文件大小为 mb。

When our QA is testing the pdf viewer he encountered this error:当我们的 QA 测试 pdf 查看器时,他遇到了这个错误:

"WebSocket closed with status code: 1006 ()" “WebSocket 以状态码关闭:1006 ()”

And the pdf viewer won't work. pdf 查看器将无法工作。 This scenario has most likely happened when using the company's VPN, as everyone is working from home, but when testing the pdf viewer when at the office using the direct network connection, the pdf viewer is working这种情况很可能发生在使用公司的 VPN 时,因为每个人都在家工作,但是在办公室使用直接网络连接测试 pdf 查看器时,pdf 查看器正在工作

Here's my code for calling the PDF viewer:这是我调用 PDF 查看器的代码:

Code behind:后面的代码:

//Get File from service
objFile = await myServices.GetFile(Id);
byte[] byteArray = objFile.Data;
PDFFilePath = await jSRuntime.InvokeAsync<string>("getBlobObject", Convert.ToBase64String(byteArray));

Javascript: Javascript:

function getBlobObject(b64Data) { function getBlobObject(b64Data) {

const blob = base64ToBlob(b64Data, 'application/pdf');
const url = URL.createObjectURL(blob);


var setpdf = url;
return setpdf;

function base64ToBlob(base64, type = "application/octet-stream") {
    const binStr = atob(base64);
    const len = binStr.length;
    const arr = new Uint8Array(len);
    for (let i = 0; i < len; i++) {
        arr[i] = binStr.charCodeAt(i);
    }
    return new Blob([arr], { type: type });
}

} }

Razor page: Razor 页面:

<div class="d-flex flex-wrap" id="div-pdf">
<iframe src="@PDFFilePath" type="application/pdf" width="100%" height="100%" style="overflow: auto;width:100%; height:746px;"></iframe>'

Is there a way to fix this?有没有办法来解决这个问题? especially when accessing the app using the company's VPN?尤其是在使用公司的 VPN 访问应用程序时? is there a better approach for this to work?有没有更好的方法来解决这个问题?

Thanks everyone and regards.谢谢大家和问候。

Idea one想法一

Make sure you have enable LongPolling .确保您已启用LongPolling

options.Transports = HttpTransportType.WebSockets | HttpTransportType.LongPolling;

From the offical doc .来自官方文档

Failed to connect via WebSockets, using the Long Polling fallback transport.使用长轮询后备传输无法通过 WebSockets 连接。 This may be due to a VPN or proxy blocking the connection.这可能是由于 VPN 或代理阻止了连接。

So if we enable LongPolling, maybe we can fix the issue by using LongPolling.所以如果我们启用 LongPolling,也许我们可以通过使用 LongPolling 来解决这个问题。

Idea two想法二

  1. Try to create a pdf file with only one page and test it under the vpn network to see if this problem can be reproduced.尝试创建一个只有一页的pdf文件,在vpn网络下测试,看能否重现这个问题。

  2. If the problem is gone, we can search more info about loading PDF file in pagination.如果问题消失了,我们可以搜索更多关于在分页中加载 PDF 文件的信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM