question

Takeshi-4630 avatar image
0 Votes"
Takeshi-4630 asked ·

How to open file saved in byte array?

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);
         }


not-supported
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DSPatrick avatar image
0 Votes"
DSPatrick answered ·

Visual studio languages are not currently supported here on QnA. I'd try asking for help in dedicated forums here.


https://social.msdn.microsoft.com/Forums/en-US/home?category=vslanguages

--please don't forget to Accept as answer if the reply is helpful--


Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows Server] Datacenter Management


Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.






· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.