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
?
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.
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.
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).
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.
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 }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With