将 .msg 文件下载到客户端服务器

Download .msg file to client server

提问人:Mohammad Talha 提问时间:11/13/2023 最后编辑:jdwengMohammad Talha 更新时间:11/20/2023 访问量:45

问:

我正在尝试将文件从服务器下载到客户端甚至单击的文件夹,但我无法处理逻辑 我尝试了身体上的路径,所以身体上的路径是完美的

例如,DownloadLocation 可以是 C:/myfolder

法典:

if (e.CommandName == "SendDraft")
{

  int index = Convert.ToInt32(e.CommandArgument);

  string JROPID = gvReleaseOrderEmail.Rows[index].Cells[1].Text.Trim();
  string ReleaseOrderNo = gvReleaseOrderEmail.Rows[index].Cells[2].Text.Trim();

  DataTable Report = Utilities.GetDataTable2("exec SP_JobEstReleaseOrderPress '" + Session["Compcode"].ToString().Trim() + "','" + JROPID.Trim() + "'");
  ReportDocument crystalReport;
  crystalReport = new ReportDocument();
  crystalReport.Load(Server.MapPath(@"~\Modules\JobOrder\Reports\JobEstReleaseOrderPress.rpt"));
  crystalReport.SetDataSource(Report);
  DataRow dr = Report.Rows[0];

  ExportOptions exportOptions = new ExportOptions();
  DiskFileDestinationOptions diskFileDestinationOptions = new DiskFileDestinationOptions();
  string pdfFilePath = Server.MapPath(@"~/Modules/JobOrder/Reports/" + ReleaseOrderNo.Trim() + ".pdf");
  diskFileDestinationOptions.DiskFileName = pdfFilePath;
  exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
  exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
  exportOptions.ExportDestinationOptions = diskFileDestinationOptions;
  crystalReport.Export(exportOptions);


  crystalReport.Close();
  crystalReport.Dispose();


  string toEmail = gvReleaseOrderEmail.Rows[index].Cells[9].Text.Trim(); // Assuming Email field is at index 8
  string subject = dr["EmailSubject"].ToString();
  string Body = dr["Body"].ToString();
  string ccEmail = dr["cc"].ToString();
  string attachmentPath = pdfFilePath; // Path to your attachment file


  try
  {
      var senders = new Sender("Nadeem", dr["MailFrom"].ToString());
      var msg = new Email(senders, subject);

      msg.Recipients.AddTo(toEmail, "Recipient Name");
      msg.Recipients.AddCc(ccEmail, "CC Recipient Name");
      msg.Subject = subject;
      msg.BodyHtml = Body;
      msg.Attachments.Add(attachmentPath);

      //This is physically path download perfectly on this path
      string msgFilePath = Server.MapPath("~/Modules/JobOrder/Reports//" + ReleaseOrderNo.ToString().Trim() + ".msg");

      if (File.Exists(msgFilePath))
      {
          File.Delete(msgFilePath);
      }


      msg.Save(msgFilePath);

      msg.Dispose();


      if (File.Exists(attachmentPath))
      {
          File.Delete(attachmentPath);
      }

      // Set up the response for file download
      byte[] buffer;
      using (FileStream fileStream = new FileStream(msgFilePath, FileMode.Open))
      {
          int fileSize = (int)fileStream.Length;
          buffer = new byte[fileSize];
          fileStream.Read(buffer, 0, fileSize);
      }

      // Set up the response for file download
      Response.Clear();
      Response.Buffer = true;
      Response.BufferOutput = true;
      Response.ContentType = "application/vnd.ms-outlook"; // Content type for .msg files
      Response.AddHeader("Content-Disposition", "attachment; filename=" + "msgFilePath.msg"); // Specify the filename
      Response.CacheControl = "public";
      Response.BinaryWrite(buffer);
      Response.End();

  }
  catch (System.Exception ex)
  {
      Logger.LogError("An error occurred: " + ex.Message);

  }
}
C# asp.net WebForms 客户端 响应

评论

0赞 Fildor 11/13/2023
“请指导我如何解决这个问题” - 什么问题?您还没有说明是什么没有按预期工作。
0赞 Mohammad Talha 11/13/2023
在客户端服务器的下载文件夹中未下载文件
0赞 Mohammad Talha 11/13/2023
我想在用户单击和事件时在客户端服务器上下载 .msg 文件
2赞 VDWWD 11/13/2023
不能强制下载到客户端计算机上的特定文件夹。您只能提供下载,仅此而已。
0赞 Fildor 11/13/2023
您还应该考虑以与操作系统无关的方式构建路径,但这并不能解决您的问题。我目前想知道写入光盘的中间步骤是否真的有必要。另外:请注意,您当前将内容文件名显式设置为(字面意思)“msgFilePath.msg”。我猜这不是你想要的。

答:

0赞 Hornwood509 11/20/2023 #1

如果您使用的是 Web 窗体,则最简单的方法是通过 Response 对象将文件移交给浏览器,并让用户决定将其保存在何处。下面的代码示例是我专门处理下载 .MSG 文件。

using (SqlDataReader rd = cm.ExecuteReader()){
Response.Clear();
while (rd.Read())
{
    if (rd["ContentType"].ToString().ToLower() == AttachmentsAndUploads.ContentType_Office_MSG)// Outlook
{
    Response.ContentType = rd["ContentType"].ToString();
        Response.AppendHeader("Content-Disposition", $"attachment;filename={rd["FileName"].ToString()}");// .msg
        Response.BinaryWrite((byte[])rd["FileData"]);
    }
}

}