private async void downloadToolStripMenuItem_Click()

in Office365APIEditor/UI/AttachmentViewerForm.cs [210:368]


        private async void downloadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string attachmentId = "";

            if (dataGridView_AttachmentList.SelectedRows == null || dataGridView_AttachmentList.SelectedRows.Count == 0)
            {
                if (dataGridView_AttachmentList.SelectedCells == null || dataGridView_AttachmentList.SelectedCells.Count == 0)
                {
                    // Attachment is not selected.
                    return;
                }
                else
                {
                    // Cell is selected but row is not selected.
                    
                    // Select the row.
                    dataGridView_AttachmentList.Rows[dataGridView_AttachmentList.SelectedCells[0].RowIndex].Selected = true;

                    // Get the attachment ID of the selected row.
                    attachmentId = dataGridView_AttachmentList.SelectedRows[0].Tag.ToString();

                    currentId = attachmentId;

                    // Reset rows.
                    dataGridView_ItemProps.Rows.Clear();
                    foreach (DataGridViewColumn col in dataGridView_ItemProps.Columns)
                    {
                        col.HeaderCell.SortGlyphDirection = SortOrder.None;
                    }

                    // Display the details of the selected attachment.
                    await ShowAttachmentDetailAsync(attachmentId);
                }
            }

            attachmentId = dataGridView_AttachmentList.SelectedRows[0].Tag.ToString();

            // Get the type of attachment.

            AttachmentType attachmentType = AttachmentType.FileAttachment;

            try
            {
                attachmentType = GetAttachmentTypeOfSelectedAttachment();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Office365APIEditor");
                return;
            }

            switch (attachmentType)
            {
                case AttachmentType.ItemAttachment:
                    if (Util.UseMicrosoftGraphInMailboxViewer)
                    {
                        saveFileDialog1.FileName = dataGridView_AttachmentList.SelectedRows[0].Cells[0].Value.ToString() + ".eml";

                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            string rawContent = "";

                            try
                            {
                                rawContent = await viewerRequestHelper.GetAttachmentRawContentAsync(targetFolder.Type, targetItemId, attachmentId);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Office365APIEditor");
                                return;
                            }

                            try
                            {
                                using (StreamWriter streamWriter = new StreamWriter(saveFileDialog1.FileName, false, System.Text.Encoding.UTF8))
                                {
                                    streamWriter.Write(rawContent);
                                }

                                MessageBox.Show("The attachment file was saved successfully.", "Office365APIEditor");
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("The selected attachment is Item Attachment. You can not download this type of attachment using Office365APIEditor", "Office365APIEditor");

                        // The request to get MIME content of itemAttachment will be like below.
                        // https://outlook.office.com/api/beta/Users('6fc42d08-123b-405e-904d-545882e8922f@6d046331-5ea5-4306-87ae-8d51f3dcc71e')/Messages('AAMkAGYxOTczODY2LTQwYzktNDFmYS05ZTIzLWZmNjAxYmM1MWYwZABGAAAAAACmFAp715xPRpcdN7o1X1D7BwDKF8masRMzQ4BmqIbV6OsxAAAAAAEMAADKF8masRMzQ4BmqIbV6OsxAAM85mvEAAA=')/Attachments('AAMkAGYxOTczODY2LTQwYzktNDFmYS05ZTIzLWZmNjAxYmM1MWYwZABGAAAAAACmFAp715xPRpcdN7o1X1D7BwDKF8masRMzQ4BmqIbV6OsxAAAAAAEMAADKF8masRMzQ4BmqIbV6OsxAAM85mvEAAABEgAQAJHp5fRvE4ZIjU_j3_4mUYI=')/$value

                    }

                    break;
                case AttachmentType.FileAttachment:
                    saveFileDialog1.FileName = dataGridView_AttachmentList.SelectedRows[0].Cells[0].Value.ToString();

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string rawContentBytes = "";

                        try
                        {
                            rawContentBytes = GetContentBytesOfSelectedAttachment();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Office365APIEditor");
                            return;
                        }

                        try
                        {
                            byte[] bytes = Convert.FromBase64String(rawContentBytes);

                            using (FileStream fileStream = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write))
                            {
                                fileStream.Write(bytes, 0, bytes.Length);
                            }

                            MessageBox.Show("The attachment file was saved successfully.", "Office365APIEditor");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    break;
                case AttachmentType.ReferenceAttachment:
                    string sourceUrl = "";

                    try
                    {
                        sourceUrl = GetSourceUrlOfSelectedAttachment();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Office365APIEditor");
                        return;
                    }

                    try
                    {
                        Process.Start(sourceUrl);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    break;
                default:
                    break;
            }
        }