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

zbq4xfa0  于 4个月前  发布在  .NET
关注(0)|答案(1)|浏览(70)

我试图从服务器下载一个文件到一个文件夹,客户端上点击,甚至,但我不能处理的逻辑,我尝试物理路径是完美的工作
例如,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);

  }
}

字符串

aoyhnmkz

aoyhnmkz1#

如果您使用的是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"]);
    }
}

字符串
}

相关问题