Ejemplo n.º 1
0
        private async void DownloadFileButton_Click(object sender, EventArgs e)
        {
            try
            {
                var selectedFileIndex = AvaibleFilesListBox.SelectedIndex;
                if (selectedFileIndex > -1 && selectedFileIndex < AvaibleFilesListBox.Items.Count)
                {
                    var fileInfo = AvaibleFilesListBox.Items[selectedFileIndex].ToString();
                    var fileId   = GetFileIdByFileInfo(fileInfo);
                    if (fileId != -1)
                    {
                        var downloadFile = await fileSharingClient.DownloadFile(fileId, FileSharingServerUrl);

                        if (downloadFile != null)
                        {
                            var saveFileDialog = new SaveFileDialog();
                            saveFileDialog.FileName = downloadFile.FileName;
                            if (saveFileDialog.ShowDialog() == DialogResult.OK)
                            {
                                var filePath = saveFileDialog.FileName;
                                using (var fileStream = new FileStream(filePath, FileMode.Create))
                                {
                                    fileStream.Write(downloadFile.FileBytes, 0, downloadFile.FileBytes.Length);
                                }
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Id файла с таким названием не найдено!", "Error!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Исключение: " + ex.Message);
            }
        }
Ejemplo n.º 2
0
        private async void btDownloadFile_Click(object sender, EventArgs e)
        {
            int selectedFileIndex = lbMessageFiles.SelectedIndex;

            if (selectedFileIndex > -1 && selectedFileIndex < lbMessageFiles.Items.Count)
            {
                int             fileId          = communityData.Dialogs[MatchingDialogs[CurrentDialog]].MessagesHistory[CurrentMessageId].AttachedFiles[selectedFileIndex];
                FileForDownload fileForDownload = await fileSharingClient.DownloadFile(fileId, FileSharingServerUrl);

                if (fileForDownload != null)
                {
                    SaveFileDialog saveDialog = new SaveFileDialog();
                    saveDialog.FileName = fileForDownload.FileName;
                    if (saveDialog.ShowDialog() == DialogResult.OK)
                    {
                        string filePath = saveDialog.FileName;
                        using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            fileStream.Write(fileForDownload.FileBytes, 0, fileForDownload.FileBytes.Length);
                        }
                    }
                }
            }
        }