通过google doc viewer在webKitBrowser中显示word文档 [英] showing a word document in webKitBrowser trough google doc viewer
问题描述
我有一个带有webKitBrowser的表单。用户应该在互联网上冲浪,当他点击* .doc链接时,必须显示word文档。当用户点击.doc链接时,我在哪里可以告诉浏览器该做什么?
更新:
我已经完成了这个(使用System.Web.dll):
namespace testGoogleDoc
{
public partial class Form1:Form
{
静态 bool IsDocumentToRedirect( string url)
{
return url.ToLower()。EndsWith( 。doc)|| url.ToLower()。EndsWith( 。docx);
} // IsDocumentToRedirect
public Form1()
{
InitializeComponent();
browser.Navigate( http://www.google.com); // BrowserHomePage
}
private void browser_Navigating( object sender,WebBrowserNavigatingEventArgs e)
{
string urlString = e.Url.ToString();
if (urlString.ToLower()。StartsWith( https://www.google.com/url?q=)|| urlString.ToLower()。StartsWith( http://www.google.com/url?q=))
{
Uri uri = new Uri(urlString);
Uri doc = new Uri(HttpUtility.ParseQueryString(uri.Query).Get( q));
urlString = doc.OriginalString.ToLower();
}
if (IsDocumentToRedirect(urlString)&&!urlString.ToLower()。StartsWith( https://docs.google.com/viewer))
{
e.Cancel = 真跨度>;
browser.Navigate( https://docs.google.com/viewer?url= + urlString);
}
}
}
}
有没有一种最好的方法来提取其他复杂URL的查询部分中的正确URL?
我至少可以看到一个问题:间接递归( http://en.wikipedia.org/wiki/Recursion [ ^ ])。
致电导航
,您的浏览器将加载另一个文档,这会导致再次调用加载
事件。递归是无限的。您需要将导航的情况与导航中的docs.google.com/viewer?url=隔离为Word文档。此外,您需要取消导航到Word文档。
执行类似
的操作(myBrowser.Url.ToString.ToLower()。StartsWith( http://docs.google。 com / viewer?url =))退出;
或类似的东西。
另外,正如我在问题评论中解释的那样修复你的正则表达,请参阅。或者,更简单,使用strong.ToLower
和string.EndsWith
,例如:
< pre lang =c#> string loUrl = myBrowser.Url.ToString.ToLower();
if (liUrl.endsWith( )。 docx)|| liUrl.endsWith( 。doc)) // ...
请参阅: http://msdn.microsoft.com/en-us/library/system.string.aspx 一> [ ^ ]。
这不是最终解决方案,但我认为非常接近。只是朝这个方向努力。
顺便说一下,我没有看到WebKit的需求。如果你只在Windows上工作,你可以使用.NET FCL中已有的WebBrowse
类之一做同样的事情。
-SA
i have a form with a webKitBrowser. The user should surf on internet, and when he click a *.doc link, the word document must be shown. where can i tell the browser what to do when the user click on a .doc link?
update:
i''ve done this (using System.Web.dll):
namespace testGoogleDoc
{
public partial class Form1 : Form
{
static bool IsDocumentToRedirect(string url)
{
return url.ToLower().EndsWith(".doc") || url.ToLower().EndsWith(".docx");
} //IsDocumentToRedirect
public Form1()
{
InitializeComponent();
browser.Navigate("http://www.google.com"); //BrowserHomePage
}
private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
string urlString = e.Url.ToString();
if (urlString.ToLower().StartsWith("https://www.google.com/url?q=") || urlString.ToLower().StartsWith("http://www.google.com/url?q="))
{
Uri uri = new Uri(urlString);
Uri doc = new Uri(HttpUtility.ParseQueryString(uri.Query).Get("q"));
urlString = doc.OriginalString.ToLower();
}
if (IsDocumentToRedirect(urlString) && !urlString.ToLower().StartsWith("https://docs.google.com/viewer"))
{
e.Cancel = true;
browser.Navigate("https://docs.google.com/viewer?url=" +urlString);
}
}
}
}
is there a best way to extract the right URL in the query part of the other complexed URL?
I can see at least one problem: indirect recursion (http://en.wikipedia.org/wiki/Recursion[^]).
When you callNavigate
, your browser gets to loading the another document, which leads to invocation of yourLoad
event again. Recursion is "infinite". You need to isolate the case of navigation to "docs.google.com/viewer?url=" from the navigation to a Word document. Also, you need to cancel navigation to a Word document.
Do something like
if (myBrowser.Url.ToString.ToLower().StartsWith("http://docs.google.com/viewer?url=")) exit;
or something like that.
Also, fix your Regex as I explained in my comment to the question, please see. Or, simpler, usestrong.ToLower
andstring.EndsWith
, like:
string loUrl = myBrowser.Url.ToString.ToLower(); if (liUrl.endsWith(".docx") || liUrl.endsWith(".doc")) //...
Please see: http://msdn.microsoft.com/en-us/library/system.string.aspx[^].
This is not a final solution, but I think very close. Just work a bit in this direction.
By the way, I don''t see a need in WebKit. If you work on Windows only, you can do the same thing with one of theWebBrowse
classes already available in .NET FCL.
—SA
这篇关于通过google doc viewer在webKitBrowser中显示word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!