将 Webview2 Source 直接设置为二进制流 [英] Set Webview2 Source directly to a binary stream
问题描述
我的应用程序中有一个 Webview2 控件,用于查看 PDF 文档.
该应用程序还存储和读取 MS SQL 服务器 PDF 数据.
I have a Webview2 Control in my application, used to view PDF documents.
The application also stores to and reads from MS SQL server PDFs data.
目前,我正在从 SQL 检索二进制数据,将它们转换为临时文件到磁盘并设置:
Currently, I am retrieving binary data from SQL, convert them to a temporary file to disk and set:
webview2.source = New Uri("file://" + filename)
到目前为止一切正常,但我当然希望在不写入和读取磁盘的情况下完成这项工作.
That's working fine so far, but of course I would like to do the job without writing and reading to and from disk.
有没有办法在不访问磁盘的情况下做同样的事情?
Is there a way to do the same without accessing the disk?
更新(按照推荐),我尝试过的.附上部分代码以便更好理解:
Update (as recommended), what I tried. With part of code for better understandg:
Dim fieldOrdinal = reader.GetOrdinal(ColumnName)
reader.Read()
Dim blob = New Byte(reader.GetBytes(fieldOrdinal, 0, Nothing, 0, 0) - 1) {}
reader.GetBytes(fieldOrdinal, 0, blob, 0, blob.Length)
Dim pdfBase64 As String = Convert.ToBase64String(blob)
Dim html As String = "<!DOCTYPE html><html><head></head><body><div>" & $"<iframe width=100% height=500 src=\" & Chr(&H22) & "data:Application/pdf;base64,{pdfBase64}\" & Chr(&H22) & ">" & "</iframe></div></body></html>"
webview2
控件显示一个框架,但没有内容
The webview2
control shows a frame, but without content
最终更新:这里(正确)到 VB 翻译和工作代码:
Final Update: Here the (correct) to VB translated and working code:
Dim html As String = "<!DOCTYPE html><html><head></head><body><div>" & $"<iframe width=100% height=500 src=""data:Application/pdf;base64,{pdfBase64}"">" & "</iframe></div></body></html>"
推荐答案
您可以将 PDF 作为 Base64 嵌入 IFRAME
,与嵌入图像的方式相同.
在这种情况下,mime 类型当然是application/pdf
.
使用 Convert.ToBase64String()<将源 PDF 字节转换为 Base64/a>,使用标准的 HTML5 标头并将 IFRAME
和/或 DIV
容器设置为所需的大小.
You can embed the PDF in an IFRAME
as Base64, the same way you embed images.
In this case, the mime type is of course application/pdf
.
Convert to Base64 your source PDF bytes with Convert.ToBase64String(), use a standard HTML5 header and set the IFRAME
and/or DIV
container to the desired size.
使用 WebView2.NavigateToString() 方法加载带有嵌入 PDF 的 HTML:
Use the WebView2.NavigateToString() method to load the HTML with the embedded PDF:
string pdfBase64 = Convert.ToBase64String([Your PDF bytes]);
string html = "<!DOCTYPE html><html><head></head><body><div>" +
$"<iframe width=100% height=500 src=\"data:application/pdf;base64,{pdfBase64}\">" +
"</iframe></div></body></html>";
webView2.NavigateToString(html);
VB.Net 版本:
Dim pdfBytes = [DataReader].GetSqlBytes([DataReader].GetOrdinal("[Column Name]")).Value
Dim pdfBase64 As String = Convert.ToBase64String(pdfBytes)
Dim html As String = "<!DOCTYPE html><html><head></head><body><div>" &
$"<iframe width=100% height=500 src=""data:Application/pdf;base64,{pdfBase64}"">" &
"</iframe></div></body></html>"
webView2.NavigateToString(html)
iframe
大小不应该这样设置,您应该使用 CSS 样式(可能还有一个简单的 JavaScript 来处理 late 重新定义包含您的 PDF).
您可以构建一个简单的 HTML 模板并按照描述填充 Base64 内容.
The iframe
size should not be set like that, you should use CSS styles (and probably a simple JavaScript to handle late redefinition of the size of the Window that contains your PDF).
You could build a simple HTML template and fill the Base64 content as described.
这篇关于将 Webview2 Source 直接设置为二进制流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!