Exemplo n.º 1
0
        public void saveAttachments(int tmSt_Id) //call me from parent form!
        {
            //if (lvAttachedFiles.Items.Count > 0)
            //{
            List <ListViewItem> newLvItems = new List <ListViewItem>();

            foreach (ListViewItem lvi in lvAttachedFiles.Items)
            {
                if (lvi.SubItems.Count == 1)     //only filename into lv -> from db
                {
                    LvFileInfo lvfi = saveAttachmentLocally(tmSt_Id, lvi.SubItems[0].Text);

                    newLvItems.Add(new ListViewItem(new string[] { lvfi.FileName, lvfi.FilePath }));
                }
                else     //path and filename into lv -> from local dir : ok
                {
                    newLvItems.Add(lvi);
                }
            }

            Delete_SampleFiles(tmSt_Id);     //delete from db

            //lvAttachedFiles.Items.Clear();
            //lvAttachedFiles.Items.AddRange(newLvItems.ToArray());

            //insert attachments into db
            foreach (ListViewItem lvi in newLvItems)
            {
                byte[] attFileBytes = File.ReadAllBytes(lvi.SubItems[1].Text);

                if (!InertIntoTable_SampleFiles(tmSt_Id, lvi.SubItems[0].Text, attFileBytes))
                {
                    MessageBox.Show("Αποτυχία αποθήκευσης του αρχείου: " + lvi.SubItems[0].Text);
                }
            }

            Close();
            //}
            //else
            //{
            //    MessageBox.Show("Δεν υπάρχουν αρχεία προς αποθήκευση!");
            //}
        }
Exemplo n.º 2
0
        LvFileInfo saveAttachmentLocally(int Id, string Filename)
        {
            LvFileInfo ret      = new LvFileInfo();
            string     tempPath = Path.GetTempPath(); //C:\Users\hkylidis\AppData\Local\Temp\

            try
            {
                if (!Directory.Exists(tempPath))
                {
                    MessageBox.Show("Σφάλμα. Παρακαλώ ελέγξτε τα δικαιώματά σας στο " + tempPath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
                return(ret);
            }

            SqlConnection sqlConn  = new SqlConnection(SqlDBInfo.connectionString);
            string        SelectSt = "SELECT [Filename], [FileContents] FROM [dbo].[SampleFiles] WHERE TmStatus_Id = @tmStatus_Id and Filename = @Filename ";
            SqlCommand    cmd      = new SqlCommand(SelectSt, sqlConn);

            try
            {
                sqlConn.Open();
                cmd.Parameters.AddWithValue("@tmStatus_Id", Id);
                cmd.Parameters.AddWithValue("@Filename", Filename);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    string realFileName = reader["Filename"].ToString().Trim();
                    //string tempFile = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + "~" + realFileName);
                    //temp file -> attachment name with temp name and tilda 'tmp123~ΦΕΚ123.pdf'
                    string tempFile = Path.Combine(tempPath, realFileName);
                    try
                    {
                        File.WriteAllBytes(tempFile, (byte[])reader["FileContents"]);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Παρουσιάστηκε πρόβλημα κατά την προσωρινή αποθήκευση του συδεδεμένου Αρχείου: '" + realFileName +
                                        "'\r\n\r\n\r\nΛεπτομέρειες:\r\n" + ex.Message);
                        try
                        {
                            tempFile = Path.Combine(tempPath, Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + "~" + realFileName);
                            File.WriteAllBytes(tempFile, (byte[])reader["FileContents"]);

                            MessageBox.Show("Προσοχή! Το αρχείο θα αποθηκευτεί με όνομα: " + tempFile);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("Προσοχή! Το αρχείο " + realFileName + " δε θα αποθηκευτεί!\r\n" + ex2.Message);
                        }
                    }

                    ret = new LvFileInfo {
                        FileName = realFileName, FilePath = tempFile
                    };
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
                return(ret);
            }

            return(ret);
        }