Reputation: 1879
I have browsed and uploaded a png/jpg file in my MVC web app. I have stored this file as byte[] in my database. Now I want to read and convert the byte[] to original file. How can i achieve this?
Upvotes: 34
Views: 97926
Reputation: 81
Or just use this:
System.IO.File.WriteAllBytes(string path, byte[] bytes)
File.WriteAllBytes(String, Byte[]) Method (System.IO) | Microsoft Docs
Upvotes: 5
Reputation: 192
May you have trouble with the mentioned solutions on DotNet Core 3.0 or higher
so my solution is:
using(var ms = new MemoryStream(yourByteArray)) {
using(var fs = new FileStream("savePath", FileMode.Create)) {
ms.WriteTo(fs);
}
}
Upvotes: 9
Reputation: 2413
Remember to reference System.Drawing.Imaging and use a using block for the stream.
Upvotes: 39
Reputation: 32278
Create a memory stream from the byte[] array in your database and then use Image.FromStream.
byte[] image = GetImageFromDatabase();
MemoryStream ms = new MemoryStream(image);
Image i = Image.FromStream(ms);
Upvotes: 28