通过google doc viewer在webKitBrowser中显示word文档 [英] showing a word document in webKitBrowser trough google doc viewer

查看:113
本文介绍了通过google doc viewer在webKitBrowser中显示word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有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 call Navigate, your browser gets to loading the another document, which leads to invocation of your Load 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, use strong.ToLower and string.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 the WebBrowse classes already available in .NET FCL.

—SA


这篇关于通过google doc viewer在webKitBrowser中显示word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆