コードビハインドでアンカーを使用し、そのアンカーをクリックするとpdfをダウンロードしようとしています。私はpageload上でうまく動作するものをダウンロードしているが、私はpdfのためにダウンロードするonlickを設定する方法がわからない。アンカータグのコードビハインドを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 "";
}
pageload上、ないにOnclickに開き、これを使用しました。ユーザーがクリックしたときにのみダウンロードしたい
私はVS 2005とSQL Server 2005でc#2.0を使用しています。コードの後ろにonclickを設定するのは本当に頭がおかしくなります。助けてくれてありがとう! PDFはブラウザ
apdf.Attributes.Add( "onclickの"、「window.location.href =で閲覧可能であるためにあなたがしたい場合
コードをもう一度見てください! – Ram