我正在尝试从服务器下载文件到客户端单击的文件夹,但我无法处理逻辑 我尝试了物理路径,所以物理路径完美工作
例如,下载位置可以是 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);
}
}
如果您使用的是 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"]);
}
}
}