Example #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            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(extraDataId, 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(extraDataId); //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(extraDataId, lvi.SubItems[0].Text, attFileBytes))
                    {
                        MessageBox.Show("Αποτυχία αποθήκευσης του αρχείου: " + lvi.SubItems[0].Text);
                    }
                }

                Close();
            }
            else
            {
                MessageBox.Show("Δεν υπάρχουν αρχεία προς αποθήκευση!");
            }
        }
Example #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], [FileCont] FROM [dbo].[SampleFiles] WHERE ExtraDataId = @ExtraDataId and Filename = @Filename ";
            SqlCommand    cmd      = new SqlCommand(SelectSt, sqlConn);

            try
            {
                sqlConn.Open();
                cmd.Parameters.AddWithValue("@ExtraDataId", 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["FileCont"]);
                    }
                    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["FileCont"]);

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