C# save binary file from stream

C# save binary file from stream

Author: Thru On: 13.06.2017

I have a StreamReader object that I initialized with a stream, now I want to save this stream to disk the stream may be a. As highlighted by Tilendor in Jon Skeet's answer, streams have a CopyTo method since.

You must not use StreamReader for binary files like gifs or jpgs. StreamReader is for text data. You will almost certainly lose data if you use it for arbitrary binary data. If you use Encoding. GetEncoding you will probably be okay, but what's the point?

Why do you need to use a StreamReader at all? Why not just keep the binary data as binary data and write it back to disk or SQL as binary data? As this seems to be something people want to see Another option is to get the stream to a byte[] and use File.

I don't get all of the answers using CopyTowhere maybe the systems using the app might not have been upgraded to. I know some would like to force people to upgrade, but compatibility is also nice, too.

Another thing, I don't get using a stream to copy from another stream in the first place. Why not just do:. This code works as I've tested it with a. One stream, no copying between streams, no encoding needed, just write the bytes!

No need to over-complicate things with StreamReader if you already have a stream you can convert to bytes directly with. Only potential downsides I can see in doing it this way is if there's a large file you have, having it as a stream and using. CopyTo or equivalent allows FileStream to stream it instead of using a byte array and reading the bytes one by one.

It might be slower doing it this way, as a result. But it shouldn't choke since the. Write method of the FileStream handles writing the bytes, and it's only doing it one byte at a time, so it won't clog memory, except that you will have to have enough memory to hold the stream as a byte[] object. In my situation where I used this, getting an OracleBlobI had to go to a byte[]it was small enough, and besides, there was no streaming available to me, anyway, so I just sent my bytes to my function, above.

Another option, using a stream, would be to use it with Jon Skeet's CopyStream function that was in another post - this just uses FileStream to take the input stream and create the file from it directly. It does not use File. Createlike he did which initially seemed to be problematic for me, but later found it was likely just a VS bug By posting your answer, you agree to the privacy policy and terms of service.

Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7. Join them; it only takes a minute: How do I save a stream to a file in C? InputStream ; I need to save this to disk I have the filename. In the future I may want to store this to SQL Server. I have the encoding type also, which I will need if I store it to SQL Server, correct? Loadman 2, 3 11 3. CopyTo fileStream ; fileStream.

Close ; Or with the using syntax: Jony Adamit 1, 11 Begin if you're not already at the beginning or you won't copy the entire stream.

Lilly Wood & The Prick and Robin Schulz - Prayer In C (Robin Schulz Remix) (Official)

If this input stream is got from http connection then will it buffer and download and then write all the make money 3d scanning from the source????? I have created PDF viewer c# save binary file from stream I am using stream, once I bind the stream and when I save the pdf file using the same stream then without using "Seek 0, SeekOrigin. Begin " I wont be able to discount stock brokers list in mumbai correct document.

NetFind

CopyTo fileStream ; this line gives an error: Doesn't close either stream. Read buffer, 0, buffer.

C# Write and Read Binary Data File - Visual C# Kicks

Jon Skeet k I see people creating byte arrays the size of the entire file, which can cause problems for big files. It's present as an extension method in. I don't think it is an extension method, but it's new in Stream class. I had it as an extension method in a utility library, but now that it's in Stream itself, my extension method doesn't get called.

It's reasonably arbitrary - a small enough value to avoid taking too much memory, and large enough to transfer a reasonable chunk at a time. It would be fine to be 16K, 32K maybe - I'd just be careful not to end up on the large object heap. Jerome 3, 16 58 Darren Corbett 6 5. Your method didn't create the stream, so it shouldn't dispose of it. Instead you need to put FileStream instead to using, otherwise it will be kept open until it is garbage collected.

c# save binary file from stream

I've found that your approach was much closer to solve my problem in Definition nifty stock market with my AWS S3 class gateway class!

This ran fine but I got a 0 KB output.

c# save binary file from stream

Instead I had to do this for the correct output: In my case, input is a MemoryStream coming from within a ZipArchive. This works well in my MVC controller action method. This helped me figure out what I was doing wrong.

However, don't forget to move to the beginning of the stream: The distinction is significant such as in multiple byte character encodings like Unicode used in. Net where Char is one or more bytes byte[n]. George 1 2. Why not use a FileStream object?

c# save binary file from stream

Create fileFullPath, int stream. Read bytesInStream, 0, int bytesInStream. Write bytesInStream, 0, bytesInStream.

Adrian 2 2. Buthrakaur is right -- this answer should be deleted. This is not working with ResponseStream, because it is of uknown length. Plus, most computers have at least 2 GB of RAM available these days, anyway Caveat is valid, but I think this is a case where it's probably "good enough" for most jobs. Major Byte 3, 3 19 Angelo 3 9. Supplying a buffered input stream directly to the FileStream - nice! CopyTo stream ; File. Why not just do: ToArray ; Once you have the bytes, you can easily write them to a file: Combine path, fileName File.

Combine path, fileNameFileMode. Write bytes, 0, int bytes. Write byteData, 0, byteData. Close ; Full Source: C FileStream Class Rj. Massimiliano Kraus 2, 7 Rajesh 4 4. I don't get the downvotes here except for the FileMode. Append should be FileMode. This is by far the best example of how to take bytes and send them to a file.

Only thing missing is that you have to take the input stream the OP mentioned and do. I can see this really just sends straight text to a file, not using an input stream, but as I said, that is rectified VERY easily.

A shame it wasn't added to the post, but not worthy of a downvote, in my opinion. MassimilianoKraus While true, it's not entirely necessary.

You can do a wFile. Flush ; to achieve the same thing at the end.

.net - How do I save a stream to a file in C#? - Stack Overflow

MassimilianoKraus That's why you wrap it in a try Sign up or log in StackExchange. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with JavaScript enabled.

Citigroup Inc: NYSE:C quotes & news - Google Finance

Note that you have to call myOtherObject. This seems like such a common case I'm surprised its not in. If the input is too large you'll get an out of memory exception. C FileStream Class Rj share improve this answer. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system