title: How can I upload a file and save it to a Stream for further preview using C#?
body: <p>Is there a way to upload a file, save it to a Stream, this Stream I will save it temporarily in a Session and, at last, I will try to preview this uploaded file that is in this Session??</p>
  
  <p>For example, a pdf file.</p>
  
  <p>Thanks!!</p>
  
  <p><strong>EDITED</strong></p>
  
  <p>Here's what I'm trying to do:</p>
  
  <pre><code>HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
  byte[] buffer = new byte[hpf.InputStream.Length];
  MemoryStream ms = new MemoryStream(buffer);
  ms.Read(buffer, 0, (int)ms.Length);
  Session["pdf"] = ms.ToArray();
  ms.Close();
  </code></pre>
  
  <p>And in another method, I'm doing this:</p>
  
  <pre><code>byte[] imageByte = null;
  
  imageByte = (byte[])Session["pdf"];
  
  Response.ContentType = "application/pdf";
  Response.Buffer = true;
  Response.Clear();
  Response.BinaryWrite(imageByte);
  </code></pre>
  
  <p>But nothing happends... my browser even opens a nem page to show the pdf file, but a window is shown saying that the file is not a pdf (or something like the file doesn't initiate with pdf, I didn't understand that)</p>