Sergey Shoshin
Sergey Shoshin

Reputation: 475

Opening file from named pipe in browser - possible or not? Without saving on the disk

I created named pipe on C#.

Server

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);
            }
        }

Client

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();
}

In this pipe exists a file. How can i open it in browser (IE) without saving on disk? I know that my file is NT object, but how open it?

Upvotes: 3

Views: 2016

Answers (1)

Żubrówka
Żubrówka

Reputation: 720

If your pictures are small enough (~32K base64 encoded) to be passed as parameters (!) and you are OK with IE8 or higher (or other browsers) you can try this:

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");

If I think about something else I'll let you know :)

Upvotes: 2

Related Questions