I am trying to open file saved in bytes. Here is my code ... how to do it correct?
public class Documents
{
public string name { get; set; }
public string ext { get; set; }
public long length { get; set; }
public byte[] document { get; set; }
}
private void btnDocument_Click(object sender, RoutedEventArgs e)
{
try
{
string init_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString();
F.OpenFileDialog file = new F.OpenFileDialog();
file.Multiselect = true;
file.Filter = "Image (*.png; *.jpg)|*.png; *.jpg|" +
"PDF file (*.pdf)|*.pdf|" +
"MS Word (*.doc; *.docx)|*.doc; *.docx|" +
"MS Excel (*.xls; *.xlsx)|*.xls; *.xlsx";
file.DefaultExt = "*.pdf";
file.InitialDirectory = init_path;
long length = 0;
byte[] array;
if (file.ShowDialog() == F.DialogResult.OK)
{
for (int i = 0; i < file.FileNames.Length; i++)
{
using (FileStream fs = File.OpenRead(file.FileNames[i]))
{
using (BinaryReader br = new BinaryReader(fs))
{
length = fs.Length;
array = br.ReadBytes((int)fs.Length);
}
}
Documents doc = new Documents();
doc.name = file.SafeFileNames[i].ToString();
doc.ext = Path.GetExtension(file.SafeFileNames[i].ToString());
doc.document = array;
doc.length = length;
dtgDocuments.Items.Add(doc);
}
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}
private void dtgDocuments_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
byte[] array = (dtgDocuments.SelectedItem as Documents).document;
FileStream file = File.Create("test_test");
file.Write(array, 0, array.Length);
}