Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need Help with Writing a IO.Stream

Need Help with Writing a IO.Stream

Scheduled Pinned Locked Moved C#
helpquestionsysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Flores
    wrote on last edited by
    #1

    I am writing a webservice that allows a user to upload files. i had no problem handling pictures but i am having trouble handling plain text files. How do I write out my stream to a text file that the webservice receives from the client? HOw do I take my System.IO.Stream and pass it into a FileStream so that I can write the file anywhere I want on the server? Thanks in advance

    H 1 Reply Last reply
    0
    • D David Flores

      I am writing a webservice that allows a user to upload files. i had no problem handling pictures but i am having trouble handling plain text files. How do I write out my stream to a text file that the webservice receives from the client? HOw do I take my System.IO.Stream and pass it into a FileStream so that I can write the file anywhere I want on the server? Thanks in advance

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Pass the Stream into the constructor of a StreamReader, then buffer the input (in blocks, for which 4096 is a good size typically) to a TextWriter derivative (perhaps a StreamWriter with a FileStream passed to its constructor that points at the right place. This will also allow you to change the encoding if you like. You could skip the readers and writers, though, and just buffer the input directly from the Stream to a FileStream, something like:

      private void Save(Stream s)
      {
      FileStream file = new FileStream("c:\temp\file.txt", ...);
      using (file)
      {
      byte[] buffer = new byte[4096];
      int read = 0;
      read = s.Read(buffer, 0, buffer.Length);
      while (read != 0)
      {
      file.Write(buffer, 0, read);
      read = s.Read(buffer, 0, buffer.Length);
      }
      }
      }

      A very simplistic example, but you should get the idea. The code would be similar using a TextReader and TextWriter derivative, but would allow you to more easily handle text as opposed to binary data (although you could still get an encoded string at any time using the Encoding class derivatives).

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups