2012-12-27 91 views
2

我在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對象,但是如何打開它? (!)

+0

開箱即用的答案可能不是,但一個解決方案可能是一個自定義的命名管道URL名稱的Internet Explorer http:// msdn .microsoft.com/en-us/library/jj650247(v = vs.85).aspx – rene

+0

恐怕這確實是臨時文件的用途。 – Gusdor

回答

2

如果你的照片足夠小(〜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"); 

如果我想別的東西我會讓你知道:)

相關問題