21

Possible Duplicate:
How do I save a stream to a file?

I have got a stream object which may be an image or file (msword, pdf), I have decided to handle both types very differently, as I may want to optimize/ compress / resize / produce thumbnails etc. I call a specific method to save an image to disk, the code:

var file = StreamObject;

//if content-type == jpeg, png, bmp do...
    Image _image = Image.FromStream(file);
    _image.Save(path);

//if pdf, word do...

How do I actually save word and pdfs?

//multimedia/ video?

I have looked (not hard enough probably) but I could not find it anywhere...

Community
  • 1
  • 1
Haroon
  • 3,402
  • 6
  • 43
  • 74
  • Look Here If any of this doesn't work. http://stackoverflow.com/questions/13100666/access-assets-from-monodroid-class-library/17824673#17824673 – Nelson Brian Jul 24 '13 at 03:26

6 Answers6

27

If you are using .NET 4.0 or newer you can use this method:

public static void CopyStream(Stream input, Stream output)
{
    input.CopyTo(output);
}

If not, use this one:

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8 * 1024];
    int len;
    while ( (len = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, len);
    }    
}

And here how to use it:

using (FileStream output = File.OpenWrite(path))
{
    CopyStream(input, output);
}
rotman
  • 1,641
  • 1
  • 12
  • 25
  • 5
    Since above code is copied from [this SO answer](http://stackoverflow.com/a/411605/678801), I believe a reference back to the original answer is in place. – Christofer Eliasson Dec 19 '13 at 14:43
  • 6
    Are you sure? Maybe from [this one](http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances)? Or maybe from [Spring.NET source](https://github.com/spring-projects/spring-net/blob/master/src/Spring/Spring.Core/Util/IoUtils.cs)? I guess this implementation is even older. Maybe I used the code from Jon's answer at the moment but I cannot remember it because it was almost 4 years ago ;) – rotman Dec 19 '13 at 20:26
23

For file Type you can rely on FileExtentions and for writing it to disk you can use BinaryWriter. or a FileStream.

Example (Assuming you already have a stream):

FileStream fileStream = File.Create(fileFullPath, (int)stream.Length);
// Initialize the bytes array with the stream length and then fill it with data
byte[] bytesInStream = new byte[stream.Length];
stream.Read(bytesInStream, 0, bytesInStream.Length);    
// Use write method to write to the file specified above
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
//Close the filestream
fileStream.Close();
Cosimo Chellini
  • 1,560
  • 1
  • 14
  • 20
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • might be better to put FileStream inside using statement and also use MemoryStream(bytesInStream) inside using statement as well. – kimbaudi Oct 06 '16 at 16:55
  • 9
    I'm voting this down as ***dangerous code***. `Stream.Read` makes no guarantee that it will fulfill a read of specified length (`bytesInStream.Length`). This is why it returns a value that represents the amount that was actually read. By ignoring this return value and not acting correctly in the case that `amtReturned < bytesInStream.Length`, this code is a dangerous maintenance hazard that may sometimes work (for short streams), but **will** blow up in your face sooner or later. **Don't use this code**. – spender Apr 13 '18 at 15:54
4

I have to quote Jon (the master of c#) Skeet:

Well, the easiest way would be to open a file stream and then use:

byte[] data = memoryStream.ToArray(); fileStream.Write(data, 0, data.Length);

That's relatively inefficient though, as it involves copying the buffer. It's fine for small streams, but for huge amounts of data you should consider using:

fileStream.Write(memoryStream.GetBuffer(), 0, memoryStream.Position);

Richard
  • 21,728
  • 13
  • 62
  • 101
2

For the filestream:

//Check if the directory exists
if (!System.IO.Directory.Exists(@"C:\yourDirectory"))
{
    System.IO.Directory.CreateDirectory(@"C:\yourDirectory");
}

//Write the file
using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(@"C:\yourDirectory\yourFile.txt"))
{
    outfile.Write(yourFileAsString);
}
Koen
  • 2,501
  • 1
  • 32
  • 43
0

if the data is already valid and already contains a pdf, word or image, then you could use a StreamWriter and save it.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 2
    example of any code? I currently got: StreamWriter filewriter = new StreamWriter(file); I want to save to a specific location on disk – Haroon Feb 15 '11 at 11:09
0

Just do it with a simple filestream.

var sr1 = new FileStream(FilePath, FileMode.Create);
                sr1.Write(_UploadFile.File, 0, _UploadFile.File.Length);
                sr1.Close();
                sr1.Dispose();

_UploadFile.File is a byte[], and in the FilePath you get to specify the file extention.

Tsabo
  • 834
  • 4
  • 9