Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert System.IO.Stream into string and then back to System.IO.Stream

I have property of Stream type

public System.IO.Stream UploadStream { get; set; } 

How can I convert it into a string and send on to other side where I can again convert it into System.IO.Stream?

like image 591
SOF User Avatar asked Nov 30 '10 17:11

SOF User


People also ask

How do you turn a stream into a string?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

What is System io stream?

Stream: System. IO. Stream is an abstract class that provides standard methods to transfer bytes (read, write, etc.) to the source. It is like a wrapper class to transfer bytes. Classes that need to read/write bytes from a particular source must implement the Stream class.

What is stream in c sharp?

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

What is stream in asp net core?

ASP.NET Core SignalR supports streaming from client to server and from server to client. This is useful for scenarios where fragments of data arrive over time. When streaming, each fragment is sent to the client or server as soon as it becomes available, rather than waiting for all of the data to become available.


2 Answers

I don't know what do you mean by converting a stream to a string. Also what's the other side?

In order to convert a stream to a string you need to use an encoding. Here's an example of how this could be done if we suppose that the stream represents UTF-8 encoded bytes:

using (var reader = new StreamReader(foo.UploadStream, Encoding.UTF8)) {     string value = reader.ReadToEnd();     // Do something with the value } 
like image 94
Darin Dimitrov Avatar answered Sep 30 '22 12:09

Darin Dimitrov


After some searching , other answers to this question suggest you can do this without knowing / using the string's encoding . Since a stream is just bytes , those solutions are limited at best . This solution considers the encoding:

    public static String ToEncodedString(this Stream stream, Encoding enc = null)     {         enc = enc ?? Encoding.UTF8;          byte[] bytes = new byte[stream.Length];         stream.Position = 0;         stream.Read(bytes, 0, (int)stream.Length);         string data = enc.GetString(bytes);          return enc.GetString(bytes);     } 

source - http://www.dotnetfunda.com/codes/show/130/how-to-convert-stream-into-string

String to Stream - https://stackoverflow.com/a/35173245/183174

like image 42
LostNomad311 Avatar answered Sep 30 '22 13:09

LostNomad311