说明:
- 当传递的参数字符过多时需将其压缩才可以进行get的请求;
- _domain是配置文件中的配置:<add key="Domain" value="http://localhost/hotelSys" /> ;
- _hqPdfFilePath同样是配置文件的PDF保存路径;
- HtmlToPdf.exe是一个程序,可以前往下载,下载地址: https://download.csdn.net/download/qq_35481871/12197661
- 在asp.net mvc中如何用return File实现下载文件时,需要注意的是将下载文件名进行编码Url.Encode(downName),否则可能出现错误或文件名乱码
protected static readonly string _domain = ConfigurationManager.AppSettings["Domain"];
/// <summary>
/// 下载Pdf行程单
/// </summary>
/// <param name="RouteBase"></param>
/// <param name="RouteList"></param>
/// <param name="DownName"></param>
/// <returns></returns>
[HttpPost]
public ActionResult DownSchedulePdf(string RouteBase, string RouteList, string DownName)
{
var downName = DownName + ".pdf";//下载名称
var pdfname = "hq" + DateTime.Now.ToString("HHmmssffff") + ".pdf";//保存pdf名称
var _pdfFilePath = _hqPdfFilePath + pdfname;//pdf文件路径
try
{
#region 压缩字符串
byte[] b1 = Compress(RouteBase);
var routeBase = Convert.ToBase64String(b1);
byte[] b2 = Compress(RouteList);
var routeList = Convert.ToBase64String(b2);
#endregion
var pageUrl = "";
if (_mvcTest == "Y")
{
pageUrl = "http://localhost" + Url.Action("SchedulePdf", "DownLoad", new { RouteBase = routeBase, RouteList = routeList });
}
else
{
pageUrl = _domain + Url.Action("SchedulePdf", "DownLoad", new { RouteBase = routeBase, RouteList = routeList });
}
//判断是否有该路径 没有则创建
if (!Directory.Exists(_hqPdfFilePath))
{
Directory.CreateDirectory(_hqPdfFilePath);
}
#region 调用html转pdf接口
//程序
string str = Server.MapPath("~/Exes/HtmlToPdf.exe");
//参数
var arguments = " \"" + pageUrl + "\" " + _pdfFilePath;
Process p = Process.Start(str, arguments);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.WaitForExit();//等待进程关闭或退出执行以下步骤
p.Close();
#endregion
}
catch (Exception e)
{
Log.InfoFormat("error:{0}", e.Message);
}
return File(_pdfFilePath, "text/plain", Url.Encode(downName));
}