How to put in my table in JSON jQuery a sql pdf?


I would like to open my pdf file that I stored in my sql table with the following fields: nombre(varchar),tipo(varchar),ruta(varbinary) . How would it be?

So I have it but it only brings me the name as I open the pdf ???

function OnSuccess(response) {

    var Data = jQuery.parseJSON(response.d);

    $('#tbDatos').append('<table id="example"><thead><tr><th style="display: none">Id </th><th>Nombre</th><th>nomArchivo</th></table>');
    var table = $('#tbDatos').children().addClass("display");
    for (var i = 0; i < Data.length; i++) {
   table.append("<tr id='row_" + i + "'><td style='display: none'>" + Data[i].id + "</td><td name='nombre'>" + Data[i].nombre "</td>" + "<td name='nomArchivo'>" + "<a href=''"; + Data[i].ruta + "target='_blank'>" + Data[i].nomArchivo + "</a></td></td></tr>")     

I already have the file stored, I just want to read it

  • put a column of the pdf file a link for example and
  • by clicking on the file name this opens the pdf.

I am using: WebForms C# SQL Server

asked by AraceLi Torres 20.06.2016 в 19:10

1 answer


If you have the route in the BD and you have it in Data , you should show a link with a blank target that points to the path of the file resulting in a file download by clicking on the link, see:

table.append("<tr id='row_" + i + "'><td style='display: none'>" + Data[i].id + "</td><td name='nombre'>" + Data[i].nombre "</td>" + "<td name='nomArchivo'>" + "<a href=''" + Data[i].ruta + " target='_blank'>" + Data[i].nomArchivo + "</a></td></td></tr>");


Now knowing in detail the characteristics of how the software is being built, here is a better answer:

First we create a Generic Handler (* .ashx) called FileCS in the root of the project with the following code:

<%@ WebHandler Language="C#" Class="FileCS" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class FileCS : IHttpHandler
    public void ProcessRequest(HttpContext context)
        //Aqui falta algo importante: saber si el usuario está autenticado para darle la seguridad del caso
        int id = int.Parse(context.Request.QueryString["IdDeMiPDF"]);
        byte[] bytesDelPDF;
        string fileName, contentType;
        //De aqui sacamos la cadena de conexion
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
            using (SqlCommand cmd = new SqlCommand())
                cmd.CommandText = "SELECT nombre, ruta, tipo FROM nombreDeLaTabla WHERE [email protected]";
                cmd.Parameters.AddWithValue("@IdDeMiPDF", id);
                cmd.Connection = con;
                using (SqlDataReader sdr = cmd.ExecuteReader())
                    bytesDelPDF = (byte[])sdr["ruta"];
                    contentType = sdr["tipo"].ToString();
                    fileName = sdr["nombre"].ToString();
        context.Response.Buffer = true;
        context.Response.Charset = "";       
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);        
        context.Response.ContentType = "application/pdf";

    public bool IsReusable
            return false;

Second, define the call to that file by sending the ID

ResolveUrl("~/FileCS.ashx?IdDeMiPDF=") //Y le agregas el ID, esto corre en el lado del servidor, tienes que ver cual es la ruta del archivo y lo llamas.

In your javascript the idea goes like this:

Change this

"<a href=''" + Data[i].ruta + "target='_blank'>" 

For something similar:

"<a href=''" + Data[i].id+ "target='_blank'>" 

Note where the FileCS.ashx file is.

Reference: link

answered by 20.06.2016 / 20:25