我試圖在代碼隱藏中使用錨點,並單擊該錨點時下載pdf。我有下載的東西在頁面加載上工作正常,但我不知道如何設置onlick下載PDF。設置onclick在錨點標籤下載pdf
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");
UPDATE:
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 "";
}
使用這個打開的頁面加載,而不是的onClick。只有在用戶點擊時才需要下載。
我在VS 2005和sql server 2005中使用c#2.0。在後面的代碼中設置onclick在我頭腦中確實是一團糟。預先感謝您的幫助!
請再次看看代碼! – Ram 2012-04-09 23:38:18