string bankcode = Session["BankCode"].ToString();
string filename = bankcode.ToUpper() + "_Bank_Link_Form.pdf";
try
{
string strURL = ResolveUrl("~/UI/doc/"+bankcode.ToLower()+".pdf");
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
Response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment;filename="+filename);
//byte[] data = req.DownloadData(Server.MapPath(strURL));
//req.DownloadFile(strURL, filename);
//response.BinaryWrite(data);
// Response.AppendHeader("Content-Disposition", "attachment; filename=SailBig.jpg");
Response.TransmitFile(Server.MapPath(strURL));
response.End();
}
catch (Exception ex)
{
}
这是我用于下载pdf文件的代码。尽管文件已下载,但出现异常。
答案 0 :(得分:0)
passwd
用于防止将其他任何内容添加到响应中,因为它可能会损坏您的文件。 Response.End()
,Server.Transfer()
,Response.End()
均引发异常。这些方法中的每一个都在内部调用Response.Redirect()
并导致Response.End()
异常。