C# 为锚定标记设置onclick-in-codebhind以下载pdf
我正在尝试使用codebehind中的锚点,并在单击锚点时下载pdf。我下载的东西在pageload上运行良好,但我不确定如何设置onlick to download for pdfC# 为锚定标记设置onclick-in-codebhind以下载pdf,c#,asp.net,html,C#,Asp.net,Html,我正在尝试使用codebehind中的锚点,并在单击锚点时下载pdf。我下载的东西在pageload上运行良好,但我不确定如何设置onlick to download for pdf HtmlAnchor apdf = new HtmlAnchor(); apdf.ID = Guid.NewGuid().ToString("N"); apdf.InnerText = dsreport.Tables[0].Rows[0]["ImageName"].ToString(); apdf.Attribut
HtmlAnchor apdf = new HtmlAnchor();
apdf.ID = Guid.NewGuid().ToString("N");
apdf.InnerText = dsreport.Tables[0].Rows[0]["ImageName"].ToString();
apdf.Attributes.Add("style", "font-weight: bold; font-size: 13px; margin: 0px;font-family: Arial; color: #1e7c9b; text-decoration: underline");
apdf.Attributes.Add("onclick", "");
对于每个onclick,我必须设置下面的代码,这样pdf只能在单击时下载
byte[] aByteArrayOfTheFile = (byte[])(dsreport.Tables[0].Rows[0]["ImageData"]);
SendAsFileToBrowser(aByteArrayOfTheFile, "application/pdf", "downloaded.pdf");
更新:
public static string SendAsFileToBrowser(byte[] File, string Type, string FileName)
{
string disp = "attachment";
if (string.IsNullOrEmpty(FileName))
{
disp = "inline";
}
// set headers
//char r = HttpContext.Current.Response;
HttpContext.Current.Response.ContentType = Type; // eg "image/Png"
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "binary/octet-stream");
HttpContext.Current.Response.AddHeader("Content-Length", File.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Disposition", disp + "; filename=" + FileName + "; size=" + File.Length.ToString());
HttpContext.Current.Response.Flush();
// write data to requesting browser
HttpContext.Current.Response.BinaryWrite(File);
HttpContext.Current.Response.Flush();
return "";
}
使用此选项将在pageload上打开,而不是在Onclick上打开。希望仅在用户单击时下载
我正在VS2005和SQLServer2005中使用c#2.0。设置onclick-in-code-behind在我的脑子里真是一团糟。提前感谢您的帮助 如果希望pdf在浏览器中可见,请尝试此操作 apdf.Attributes.Add(“onclick”,“window.location.href=”../linktopdf/thepff.pdf;”) 如果您想下载pdf,可以创建一个ashx web服务,将内容类型响应设置为application/pdf作为响应,然后从那里以“是”的形式下载。然后会出现下载窗口 以下是一个指向有用线程的链接:
如果希望pdf在浏览器中可见,请尝试此操作 apdf.Attributes.Add(“onclick”,“window.location.href=”../linktopdf/thepff.pdf;”) 如果您想下载pdf,可以创建一个ashx web服务,将内容类型响应设置为application/pdf作为响应,然后从那里以“是”的形式下载。然后会出现下载窗口 以下是一个指向有用线程的链接:
哦,我知道你现在需要什么了。试试这个:
apdf.Click += new EventHandler(this.SendAsFileToBrowser);
container.Controls.Add(apdf);
并确保将此代码放置在页面的Pre_Init方法中,而不是页面加载中
试试这个,告诉我进展如何。哦,我知道你现在需要什么了。试试这个:
apdf.Click += new EventHandler(this.SendAsFileToBrowser);
container.Controls.Add(apdf);
并确保将此代码放置在页面的Pre_Init方法中,而不是页面加载中
试试这个,让我知道它是怎么回事。我有一个按钮OnClick事件的HTMLAnchor代码。因此,每当我点击按钮,它是下载pdf,但相反,我想显示链接,只有当链接被点击时,pdf应该被打开。我希望我清楚,凯文。谢谢你的时间。我有一个按钮点击事件的HTMLAnchor代码。因此,每当我点击按钮,它是下载pdf,但相反,我想显示链接,只有当链接被点击时,pdf应该被打开。我希望我清楚,凯文。谢谢你抽出时间。