生成pdf下载问题。。。。。。。。。。
我有一个页面是把数据生成pdf下载的。但有个问题是:如果客户装了pdf软件,有的就会直接在IE中打开。现在我想只能下载,不要在IE中打开要怎么做? 附上下载的代码。
Response.AddHeader("Content-Disposition", "inline; filename=CalcTariff.pdf");
Response.AddHeader("Content-Length", ms.Length.ToString());
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("big5");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
Response.BinaryWrite(ms.ToArray());
Response.End();
------解决方案--------------------下载时,target转到一个页面,或者转到一个 .ashx的简单页,然后代码中以类似如下代码编写:
FileStream fs = new FileStream(dir, System.IO.FileMode.Open);
context.Response.Buffer = false;
context.Response.AddHeader("Connection", "Keep-Alive");
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(dir));
context.Response.AddHeader("Content-Length", fs.Length.ToString());
using (fs)
{
byte[] data = new byte[1024];
int byteReads;
do
{
byteReads = fs.Read(data, 0, data.Length);
context.Response.BinaryWrite(data);
} while (byteReads > 0);
}
context.Response.End();
------解决方案--------------------C# code
string FullFileName = "";
try
{
string FileName = "acrobat.pdf";
FullFileName = Server.MapPath(FileName);
//FileName--要下载的文件名
FileInfo DownloadFile = new FileInfo(FullFileName);
if (DownloadFile.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
else
{
//文件不存在
}
}
catch
{
//打开时异常了
}
------解决方案--------------------
http://ufo-crackerx.blog.163.com/blog/static/113078778201211503317177/