asp.net - 下载我已经创建 ASP.Net 的 PDF
问题描述
长话短说,我想创建将 PDF 文件打印到用户计算机的按钮所以首先,我在服务器数据库/文件中创建 PDF 文件,然后将其下载给用户。
使用此代码,我可以在服务器中创建 PDF 文件,然后搜索如何使其可下载给用户。到目前为止,没有运气,大多数错误都说“......是物理路径,但应该是虚拟路径。”
这是代码
private void cetak_pdf(string s_id, string CompanyCode , string zpath)
{
bool SUCCESS = true;
string sErrMsg = "";
string sFileName = "";
DiskFileDestinationOptions diskOpts = null;
try
{
sFileName = s_id;
diskOpts = new DiskFileDestinationOptions();
diskOpts.DiskFileName = Server.MapPath("~" + "\\pdf\\" + zpath + CompanyCode + "_" + sFileName + ".pdf");
//Response.Write(diskOpts.DiskFileName);Response.End();
// this is the file created E:\GIA_25\pdf\CompanyAddress00000003_003.pdf
rptDoc.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
rptDoc.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
rptDoc.ExportOptions.DestinationOptions = diskOpts;
rptDoc.Export();
Response.ContentType = "application/pdf";
//Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");
Response.TransmitFile(Server.MapPath("E:\\GIA_25\\pdf\\CompanyAddress00000003_003.pdf"));
Response.End();
}
catch (Exception ex)
{
SUCCESS = false;
sErrMsg = ex.Message;
throw new Exception(sErrMsg);
}
}
请帮忙,这种语言有点新
解决方案
对不起我的错:(这是使它可下载的正确代码
Response.ContentType = "APPLICATION/OCTET-STREAM";
// String Header = "Attachment; Filename=XMLFile.xml";
String Header = "Attachment; Filename=" + "CompanyAddress" + CompanyCode + "_" + sFileName + ".pdf";
Response.AppendHeader("Content-Disposition", Header);
System.IO.FileInfo Dfile = new System.IO.FileInfo(Server.MapPath("~" + "\\pdf\\" + "CompanyAddress" + CompanyCode + "_" + sFileName + ".pdf"));
Response.WriteFile(Dfile.FullName);
推荐阅读
- c - 立即发送到 TCP 套接字 bufferevent_write bufferevent libevent
- azure - 如何获取使用 Azure CLI 安装操作系统的 Azure VMS 列表?
- python - 如何在熊猫数据框列python中将1添加到所有数字==到0
- onclicklistener - 哪个方法负责点击mapbox多边形android?问二:用哪个list比较好(linked或者arrayListof和hashMap)
- laravel - Laravel required_without 使用电子邮件或数字验证进行验证
- amazon-web-services - 从公共互联网访问在 aws ec2 实例上本地运行的 web api
- jenkins - 如何通过jenkins在groovy中并行执行多个curl调用?
- python - Pandas `read_sql` SUM 列与 CLI 输出不同
- git - git 二进制存储修订优化
- activemq - ActiveMQ 经典控制台持久订阅