c# - 在浏览器中从命名管道打开文件 - 可能与否?不保存在磁盘上

标签 c# internet-explorer named-pipes nt

我在 C# 上创建了命名管道。

服务器

using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.Out))
        {
            Console.WriteLine("NamedPipeServerStream object created.");
            Console.Write("Waiting for client connection...");
            pipeServer.WaitForConnection();
            Console.WriteLine("Client connected.");
            try
            {
                using (BinaryWriter sw = new BinaryWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    Console.Write("Enter text: ");
                    byte[] bytes = File.ReadAllBytes(@"C:\\Temp\test.png");
                    sw.Write(bytes);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
        }

客户端

using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
        {
        Console.Write("Attempting to connect to pipe...");
        pipeClient.Connect();
        Console.WriteLine("Connected to pipe.");
        Console.WriteLine("There are currently {0} pipe server instances open.", pipeClient.NumberOfServerInstances);
        using (BinaryReader sr = new BinaryReader(pipeClient))
        {
            byte[] list;
            list = sr.ReadBytes(214);
        }
    }
    Console.Write("Press Enter to continue...");
    Console.ReadLine();
}

在此管道中存在一个文件。如何在不保存在磁盘上的情况下在浏览器(IE)中打开它? 我知道我的文件是 NT 对象,但如何打开它?

最佳答案

如果你的图片足够小(~32K base64 编码)可以作为参数传递(!)并且你可以使用 IE8 或更高版本(或其他浏览器)你可以试试这个:

string base64String = Convert.ToBase64String(bytes);

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
object Empty = 0;
object URL = "about:blank";
IE.Visible = true;
IE.Navigate2(ref URL, ref Empty, ref Empty, ref Empty, ref Empty);
IHTMLDocument2 hTMLDocument = (mshtml.IHTMLDocument2)IE.Document;
HTMLWindow2 iHtmlWindow2 = (HTMLWindow2) hTMLDocument.Script ;
iHtmlWindow2.execScript("document.write(\"<img src=\\\"data:image/png;base64," + base64String + "\\\">\")", "javascript");

如果我有其他想法,我会告诉你:)

关于c# - 在浏览器中从命名管道打开文件 - 可能与否?不保存在磁盘上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14052615/

相关文章:

javascript - 无法在 IE6+ 中运行的脚本、CSS 等?

jquery - IE不允许上传文件,如果不是键盘点击输入类型=文件

c++ - 无法在 vista 下重新创建命名管道

c# - 将javascript代码注入(inject)jpg图像并在用c#上传时执行代码

C#串口通信协议(protocol)

javascript - 尝试更改 IE 中 iFrame 的背景颜色

c++ - 如何创建仅在您的计算机上可访问的命名管道? (VS08 C++)

c++ - ifstream 管道和多个(顺序)编写器

c# - 是否可以在 Azure 搜索索引中填充集合?

c# - 创建具有动态字段的数据库