Create WebService to read PDF from folder and download using C# and VB.Net in ASP.Net

Last Reply 11 months ago By dharmendr

Posted 11 months ago

I am receving JSON  response  from the webservice and there is a pdf string that is to be converted  base64.How can I  bind  the pdf  to  gridview.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

      <Columns>
           <asp:TemplateField HeaderText = "S.no" ItemStyle-Width="100">
        <ItemTemplate>
            <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
       
      
           <asp:BoundField ItemStyle-Width="150px" DataField="EMPLOYEE_NAME" HeaderText="Employee Name" />
           <asp:BoundField ItemStyle-Width="150px" DataField="FATHER_NAME" HeaderText="Father's Name " />
           <asp:BoundField ItemStyle-Width="150px" DataField="POSTED_DEPARTMENT" HeaderText="Posted Department" />
           <asp:BoundField ItemStyle-Width="150px" DataField="DATE_OF_BIRTH" HeaderText="Date Of Birth" />
          
        
            <asp:TemplateField HeaderText = "data" ItemStyle-Width="100" Visible="false">
        <ItemTemplate>
            <asp:Label ID="lbldata" Text='<%#Eval("pdfString")%>' runat="server" ClientIDMode="static" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText = "pdf" ItemStyle-Width="100">
        <ItemTemplate>
             <a id='dwnldLnk'  style="display:none;" />
        </ItemTemplate>
        </asp:TemplateField>  
            <asp:TemplateField HeaderText = "pdf2" ItemStyle-Width="100">
        <ItemTemplate>
             <a href="#" onclick="downloadPDF();" title='IPR.pdf'>Download</a>
        </ItemTemplate>
        </asp:TemplateField>
          
    </Columns>
</asp:GridView>

 

if (item[i].key == "pdfString")
{
    emp.pdfString  = item[i].value;
}

 Here I am getting  pdf String

Posted 11 months ago

Hi pvermacs,

Check this example. Now please take its reference and correct your code.

I have created a web service that return base64String.

PDFService

C#

[WebMethod]
public string GetData()
{
    string path = Server.MapPath("~/Files/Test.pdf");
    byte[] bytes = System.IO.File.ReadAllBytes(path);
    return Convert.ToBase64String(bytes);
}

VB.Net

<WebMethod>
Public Function GetData() As String
    Dim path As String = Server.MapPath("~/Files/Test.pdf")
    Dim bytes As Byte() = System.IO.File.ReadAllBytes(path)
    Return Convert.ToBase64String(bytes)
End Function

Acccessing the service in page.

HTML

<asp:LinkButton Text="Download" runat="server" OnClick="Download" />

Code

C#

protected void Download(object sender, EventArgs e)
{
    PDFService service = new PDFService();
    string base64String = service.GetData();
    byte[] bytes = Convert.FromBase64String(base64String);
    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Test.pdf");
    ms.WriteTo(Response.OutputStream);
    Response.End();
}

VB.Net

Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs)
    Dim service As PDFService = New PDFService()
    Dim base64String As String = service.GetData()
    Dim bytes As Byte() = Convert.FromBase64String(base64String)
    Dim ms As IO.MemoryStream = New IO.MemoryStream(bytes)
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Test.pdf")
    ms.WriteTo(Response.OutputStream)
    Response.[End]()
End Sub

On the Download button click pdf file will be downloaded.