Refer the below sample code for your reference and implement it as per your code logic.
HTML
<div>
<asp:LinkButton ID="lnkView" runat="server" Text="View PDF" OnClick="View"></asp:LinkButton>
</div>
C#
protected void View(object sender, EventArgs e)
{
string path = Server.UrlDecode(@"C:\PDF\Test.pdf");
if (!(new FileInfo(path).Exists))
return;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
SetStream(fs);
}
}
private void SetStream(Stream stream)
{
byte[] bytes = new byte[(int)stream.Length];
string fileName = "fileName.pdf";
stream.Read(bytes, 0, (int)stream.Length);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.End();
}
VB.Net
Protected Sub View(ByVal sender As Object, ByVal e As EventArgs) Handles lnkView.Click
Dim path As String = "C:\PDF\Test.pdf"
If Not (New FileInfo(path).Exists) Then Return
Using fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)
SetStream(fs)
End Using
End Sub
Private Sub SetStream(ByVal stream As Stream)
Dim bytes As Byte() = New Byte(CInt(stream.Length) - 1) {}
Dim fileName As String = "fileName.pdf"
stream.Read(bytes, 0, CInt(stream.Length))
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename=" & fileName)
Response.BinaryWrite(bytes)
Response.End()
End Sub
Screenshot
