public void FileDownLoad(DataReport.geReportKbn rptKbn, string pgId, string[] prm) { try { this.saveDialog = new SaveFileDialog(); switch (rptKbn) { case DataReport.geReportKbn.Download: this.saveDialog.DefaultExt = ".pdf"; this.saveDialog.Filter = "All Files|*.*|Pdf Files|*.pdf"; break; case DataReport.geReportKbn.Csv: this.saveDialog.DefaultExt = ".csv"; this.saveDialog.Filter = "All Files|*.*|Csv Files|*.csv"; break; case DataReport.geReportKbn.None: this.saveDialog.Filter = "All Files|*.*"; break; default: break; } this.saveDialog.FilterIndex = 2; } catch (Exception ex) { ExMessageBox.Show("ファイル保存処理(ダイアログ表示)で予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message); if (this.utlParentFKey != null) { this.utlParentFKey.IsEnabled = true; } return; } bool?dialogResult = this.saveDialog.ShowDialog(); if (dialogResult == true) { try { string downLoadFileName = ""; string downLoadFilePath = ""; downLoadFileName = System.Windows.Browser.HttpUtility.UrlEncode(prm[0]); downLoadFilePath = prm[1].Replace(prm[0], System.Windows.Browser.HttpUtility.UrlEncode(prm[0])).Replace(@"\", "@AAB@").Replace("/", "@AAD@");; var requestUri = string.Format("{0}?rptKbn={1}&pgId={2}&random={3}&downLoadFileName={4}&downLoadFilePath={5}", Common.gstrReportDownloadUrl, ((int)rptKbn).ToString(), pgId, Common.gstrSessionString, downLoadFileName, downLoadFilePath); var client = new WebClient(); client.OpenReadCompleted += OnFileDownloadCompleted; client.OpenReadAsync(new Uri(requestUri), null); switch (rptKbn) { case DataReport.geReportKbn.OutPut: case DataReport.geReportKbn.Download: Common.gstrProgressDialogTitle = "PDFファイルダウンロード中"; break; case DataReport.geReportKbn.Csv: Common.gstrProgressDialogTitle = "CSVファイルダウンロード中"; break; } if (dlgWin == null) { dlgWin = new Dlg_Progress(); } dlgWin.Show(); } catch (Exception ex) { ExMessageBox.Show("ファイルダウンロード処理で予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message); if (this.utlParentFKey != null) { this.utlParentFKey.IsEnabled = true; } return; } } else { this.saveDialog = null; if (this.utlParentFKey != null) { this.utlParentFKey.IsEnabled = true; } GC.Collect(); } }
private void OnFileDownloadCompleted(object sender, OpenReadCompletedEventArgs e) { try { if (e.Error != null) { ExMessageBox.Show("ダウンロードファイル保存処理で通信エラーが発生しました。" + Environment.NewLine + e.Error); } else { try { byte[] data = ReadBinaryData(e.Result); string _msg = ""; try { ExUTF8Encoding _encode = new ExUTF8Encoding(); _msg = _encode.OnGetString(data, 0, 200); if (_msg.Length > 25) { if (_msg.IndexOf("error message start ==>") != -1) { MessageBox.Show("ダウンロードファイル保存処理で予期せぬエラーが発生しました。" + Environment.NewLine + _msg.Replace("error message start ==>", "")); return; } } } catch (Exception ex) { } using (System.IO.Stream fs = (System.IO.Stream) this.saveDialog.OpenFile()) { fs.Write(data, 0, data.Length); fs.Close(); } } catch (Exception ex) { ExMessageBox.Show("ダウンロードファイル保存処理で予期せぬエラーが発生しました。" + Environment.NewLine + ex.Message); return; } } } finally { if (this.utlParentFKey != null) { this.utlParentFKey.IsEnabled = true; } this.saveDialog = null; if (dlgWin != null) { dlgWin.Close(); dlgWin = null; } GC.Collect(); } }