[英]Windows.Forms.WebBrowser loading page with local SVG file
I have an HTML page that generating in C# project. 我有一个在C#项目中生成的HTML页面。 it works, when I open this page in IE.
当我在IE中打开此页面时,它可以工作。
<!DOCTYPE HTML> <meta http-equiv='X-UA-Compatible' content='IE=10' /> <html lang='en'> <head> <title>TemplateSVG</title> <script type='text/javascript' src='InterfaceSVG.js'></script> </head> <body style='margin: 0; overflow: hidden;'> <div class="page-content"> <object id='idSVG' type='image/svg+xml' data='D:\\Examples\\ExampleFla.svg'></object> </div> </body> </html>
I loaded getting text in Web browser 我已加载在Web浏览器中获取文本
if (webBrowser.Document == null)
{
webBrowser.DocumentText = theHTMLtext;
}
else
{
webBrowser.Document.OpenNew(true);
webBrowser.DocumentText = theHTMLtext;
}
But file InterfaceSVG.js isn't find. 但是找不到文件InterfaceSVG.js 。
When I give full Path to js file src='D:\\[Path]\\InterfaceSVG.js'
JS script generate exception on line with getSVGDocument()
. 当我将完整路径提供给js文件时,
src='D:\\[Path]\\InterfaceSVG.js'
脚本会与getSVGDocument()
一起生成异常。
var SvgDoc; window.addEventListener('load', function () { SvgDoc = document.getElementById("idSVG"); if (SvgDoc == null) { alert("error"); return; } SvgDoc = SvgDoc.getSVGDocument(); // IE created Access Deny. });
Edit : Try to insert text from js file. 编辑 :尝试从js文件插入文本。
<script>Text from InterfaceSVG.js </scipt>
But it generates the same exception ( Access Deny ) on line with getSVGDocument()
但它会与
getSVGDocument()
一起生成相同的异常( Access Deny getSVGDocument()
I saved result HTML page in a folder with SVG file and use function Navigate
instead of DocumentText
. 我将结果HTML页面保存在带有SVG文件的文件夹中,并使用功能
Navigate
代替DocumentText
。 Now it works... but I don't want to write on disk anything. 现在它可以工作了……但是我不想在磁盘上写任何东西。
string path = Path.GetDirectoryName(pathToSvgFile);
string file = "\\"+Path.GetFileNameWithoutExtension(pathToSvgFile);
string newfile = path + file;
File.WriteAllText(newfile, theHTMLtext);
webBrowser.Navigate(newfile);
I found how need to open a user Page. 我发现了如何打开用户页面。
<!DOCTYPE HTML> <meta http-equiv='X-UA-Compatible' content='IE=10' /> <html lang='en'> <head> <title>Empty Page</title> </head> <body style='margin: 0; overflow: hidden; '> <div class="page-content"> <object id='idSVG' style='height: 100%; width: 100%; position:absolute;' type='image/svg+xml' data='{0}'></object> </div> </body> </html>
Add script files and change text body of HTML page what you need in event webBrowser.Navigated. 添加脚本文件并更改HTML页面的文本正文,这在事件webBrowser.Navigated中需要。
static string PathToSvgFile; public static void OpenSVG(this WebBrowser webBrowser, string pathToSvgFile) { if (webBrowser.ReadyState == WebBrowserReadyState.Complete || webBrowser.ReadyState == WebBrowserReadyState.Uninitialized) { webBrowser.Navigate([Path to emptyPage.html]); PathToSvgFile = pathToSvgFile; webBrowser.Navigated += webBrowser_Navigated; } } private static void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { var webBrowser = ((WebBrowser)sender); HtmlElementCollection head = webBrowser.Document.GetElementsByTagName("head"); if (head != null) { var element = webBrowser.Document.CreateElement("script"); element.SetAttribute("type", "text/javascript"); var elementDom = (MSHTML.IHTMLScriptElement)element.DomElement; elementDom.src = [JavaScriptFile.js]; ((HtmlElement)head[0]).AppendChild(element); } webBrowser.Document.Body.InnerHtml = String.Format(webBrowser.Document.Body.InnerHtml, PathToSvgFile); webBrowser.Navigated -= webBrowser_Navigated; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.