如何使用C#.net在Web浏览器中显示文件 [英] how to display a file in a web browser using C#.net

查看:257
本文介绍了如何使用C#.net在Web浏览器中显示文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在调用Web服务以获取字节数组流,并在获取此Byte流之后,需要将字节流转换为base64string并将其保存到具有适当文件格式的本地文件中,并且需要在网络浏览器中打开该文件.

谢谢
正如SAK所指出的那样,您确实需要在此处说明使用的技术,WPF或WinForms.

而且,最重要的是您需要告诉我们:

1.您写道:我正在调用Web服务以获取字节数组流,并在获取此Byte流之后,我需要将字节流转换为base64string并将其保存为具有适当文件格式的本地文件."

这个精心设计的转换系统向我建议...如果我们确切地知道原始数据的格式,我们可能"能够帮助您减少此处描述的过程的复杂性.如果源已经是某种文件:它是什么类型的文件?

2. 您最终要在WebBrowser中显示的文件类型格式是:HTML ...还是?

...导致...

我确信您已经知道要通过Web浏览器中的VSTO Automation显示Office文件,或者使用Adobe api来显示PDF文件的策略.您可能知道第3位.来回转换RTF和HTML的第三方商业工具.

您还知道吗,在WinForms和WPF中,WebBrowser控件都可以直接显示某些不是HTML的文件类型... b ...例如:图形文件,.png,.jpg .并且,文本文件.在WPF中,根据您的安全设置(我想),您可能会弹出一个对话框,询问您是否要下载"文件:在具有相同安全设置的同一台计算机上的WinForms中,我不会获得此对话框.

我有一段时间没有看到WebBrowser控件的当前版本将显示的所有其他非HTML文件类型:上一次,我在WinForms中试图显示RTF文件只会启动默认的RTF查看器,这不足为奇.

在WinForms中,可以将WebBrowser控件的URL属性设置为有效的文件路径:在WPF中,可以将WebBrowser控件的Source属性设置为有效的路径文件路径.
示例:在WPF中:

<window x:class="TestWBinWPF.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<grid>
    <webbrowser height="311" horizontalalignment="Left" name="webBrowser1">
    VerticalAlignment="Top" Width="503"
    Source="C:\Users\Ur\Desktop\Graphics\Bill.png" />
</webbrowser></grid>
</window>

在WinForms中(Designer.cs视图):这可能是您生成的内容:

//
// webBrowser1
// 
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(1166, 552);
this.webBrowser1.TabIndex = 0;
this.webBrowser1.Url = new System.Uri("file:///C:\\Users\\Ur\\Desktop\\Graphics\\Bill.png", System.UriKind.Absolute);
//


Hi
I am calling the webservice for getting the byte array stream and after getting this Byte stream , i need to convert the byte stream to base64string and save this into local file with appropriate file format and that file, i need to open in a web browser.

Thanks
Chiranjeevi

解决方案

As SAK points out: you really need to clarify which technology, WPF, or WinForms, you are using here.

And, most important you need to tell us:

1. You wrote: "I am calling the webservice for getting the byte array stream and after getting this Byte stream , i need to convert the byte stream to base64string and save this into local file with appropriate file format."

This elaborate transformation system suggests to me ... that if we knew exactly what format the original data was in, we "might" be able to help you reduce the complexity of the process you describe here. If the source is already some kind of file: what type of file is it ?

2. what the file-type format you are going to display ultimately in the WebBrowser is: is it HTML ... or ?

... which leads to ...

I am sure you are already aware of displaying Office files via VSTO Automation in a a Web-Browser, or strategies to show PDF files using Adobe''s api''s. You are probably aware there are 3rd. party commercial tools that will convert RTF and HTML back-and-forth.

Are you, also, aware that in both WinForms and WPF the WebBrowser Control can display certain file types ... that are NOT HTML ... directly: for example, graphics files, .png, .jpg. And, text files. In WPF you may get a dialog pop-up, based on your security settings (I guess) that asks you if want to "download" the file: I do not get this dialog in WinForms on the same machine with the same security settings.

I have not looked, in a while to see all the other non-HTML file types the currrent versions of the WebBrowser Control will display: last time I looked, in WinForms, trying to display an RTF file would simply launch the default RTF viewer, which is not surprising.

In WinForms, you can set the ''URL property of the WebBrowser Control to a valid file path: in WPF you can set the ''Source property of the WebBrowser Control to a valid path file path.

Example: in WPF:

<window x:class="TestWBinWPF.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<grid>
    <webbrowser height="311" horizontalalignment="Left" name="webBrowser1">
    VerticalAlignment="Top" Width="503"
    Source="C:\Users\Ur\Desktop\Graphics\Bill.png" />
</webbrowser></grid>
</window>

In a WinForms (Designer.cs view): here''s what you might see generated:

//
// webBrowser1
// 
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(1166, 552);
this.webBrowser1.TabIndex = 0;
this.webBrowser1.Url = new System.Uri("file:///C:\\Users\\Ur\\Desktop\\Graphics\\Bill.png", System.UriKind.Absolute);
//


这篇关于如何使用C#.net在Web浏览器中显示文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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