How to convert filr into arrary of byte and save again it in file.

//Convert file in arrary of byte
String filepath = Server.MapPath(AsyncFileUpload1.FileName);
byte[] FileByteStream = File.ReadAllBytes(filepath)

//Save arrary of byte into file.
public void SaveFileFromBytes(string FileName, byte[] FileByteStream)
        {
            FileStream targetStream = null;

            Stream sourceStream = new MemoryStream(FileByteStream);

            string uploadFolder = @"~\MyDestinationFolder\";

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Server.MapPath(uploadFolder));
            if (!dir.Exists)
                dir.Create();

            string filename = FileName;

            string filePath = Path.Combine(Server.MapPath(uploadFolder), filename);

            using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {

                const int bufferLen = 4096;

                byte[] buffer = new byte[bufferLen];

                int count = 0;

                while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                {

                    targetStream.Write(buffer, 0, count);

                }

                targetStream.Close();

                sourceStream.Close();

            }
        }

Comments

Popular posts from this blog

How to create RSS Feed in Asp.Net

Auto generate change scripts in SQL Server Management Studio SSMS for tables

HOW TO Use the NumericUpDown Control