Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 链接到pdf文件(asp.net)_C#_Asp.net - Fatal编程技术网

C# 链接到pdf文件(asp.net)

C# 链接到pdf文件(asp.net),c#,asp.net,C#,Asp.net,我已使用文件上载将pdf文件保存到数据库中。现在我想从数据库中检索pdf文件,它必须链接到动态创建的链接按钮。因此,对于每个链接按钮,我都有一个链接到它的pdf文件。-如何使用C在asp.net中执行此操作首先必须从数据库中读取记录 假设您有以下表格结构: Id、名称、二进制PDFDATA 您可以使用ADO.NET、Linq2SQL或任何您正在使用的方法来选择Id和名称 输入到IEnumerable中,例如列表或数据集 然后将其绑定到ASP Repeater,其中ItemTemplate 包含链

我已使用文件上载将pdf文件保存到数据库中。现在我想从数据库中检索pdf文件,它必须链接到动态创建的链接按钮。因此,对于每个链接按钮,我都有一个链接到它的pdf文件。-如何使用C在asp.net中执行此操作首先必须从数据库中读取记录

假设您有以下表格结构:

Id、名称、二进制PDFDATA

您可以使用ADO.NET、Linq2SQL或任何您正在使用的方法来选择Id和名称 输入到IEnumerable中,例如列表或数据集

然后将其绑定到ASP Repeater,其中ItemTemplate 包含链接按钮,单击事件的代码隐藏 然后将您重定向到某个下载页面,例如downloadpdf.aspx?id={0}

其中{0}是记录的Id

download.aspx页面从数据库中读取指定的记录 并将二进制pdf数据放入缓冲区数组中

接下来,您必须设置内容类型等

我没有时间构建一个好的示例,但您可能需要:

Response.Clear()

//set the content type to PDF
Response.ContentType = "application/pdf"

//add content type header 
Response.AddHeader("Content-Type", "application/pdf")

//set the content disposition
Response.AddHeader("Content-Disposition", "inline;filename=helloworld.pdf")

//write the buffer with pdf file to the output
Response.BinaryWrite(Buffer)

Response.End()

首先,您必须从数据库中读取记录

假设您有以下表格结构:

Id、名称、二进制PDFDATA

您可以使用ADO.NET、Linq2SQL或任何您正在使用的方法来选择Id和名称 输入到IEnumerable中,例如列表或数据集

然后将其绑定到ASP Repeater,其中ItemTemplate 包含链接按钮,单击事件的代码隐藏 然后将您重定向到某个下载页面,例如downloadpdf.aspx?id={0}

其中{0}是记录的Id

download.aspx页面从数据库中读取指定的记录 并将二进制pdf数据放入缓冲区数组中

接下来,您必须设置内容类型等

我没有时间构建一个好的示例,但您可能需要:

Response.Clear()

//set the content type to PDF
Response.ContentType = "application/pdf"

//add content type header 
Response.AddHeader("Content-Type", "application/pdf")

//set the content disposition
Response.AddHeader("Content-Disposition", "inline;filename=helloworld.pdf")

//write the buffer with pdf file to the output
Response.BinaryWrite(Buffer)

Response.End()

我将编写一个通用处理程序,它将从给定id的数据库中获取PDF:

public class PdfHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int id;
        if (int.TryParse(context.Request["id"], out id)) 
        {
            id = 0;
        }

        var connectionString = ConfigurationManager.ConnectionStrings["some_db"].ConnectionString;
        using (var connection = new SqlConnection(connectionString))
        using (var command = connection.CreateCommand())
        {
            connection.Open();
            command.CommandText = "select image from some_table where image_id = :id";
            command.Parameters.AddWithValue("id", id);
            using (var reader = command.ExecuteReader())
            {
                if (reader.Read()) 
                {
                    context.Response.ContentType = "application/pdf";
                    var cd = new ContentDisposition();
                    cd.FileName = "test.pdf";
                    cd.Inline = true;
                    context.Response.AddHeader("Content-Disposition", cd.ToString());

                    long bytesRead;
                    int size = 1024;
                    var buffer = new byte[size];
                    long dataIndex = 0;
                    while ((bytesRead = reader.GetBytes(0, dataIndex, buffer, 0, buffer.Length)) > 0)
                    {
                        var actual = new byte[bytesRead];
                        Buffer.BlockCopy(buffer, 0, actual, 0, (int)bytesRead);
                        context.Response.OutputStream.Write(actual, 0, actual.Length);
                        dataIndex += bytesRead;
                    }
                }
                else
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Not found");
                    context.Response.StatusCode = 404;
                }
            }
        }
    }

    public bool IsReusable
    {
        get { return false; }
    }
}
在aspx页面中,只需放置引用此处理程序的锚:

<a href="/PdfHandler.ashx?id=1">pdf 1</a>
<a href="/PdfHandler.ashx?id=2">pdf 2</a>
<a href="/PdfHandler.ashx?id=3">pdf 3</a>
...

我将编写一个通用处理程序,它将从给定id的数据库中获取PDF:

public class PdfHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        int id;
        if (int.TryParse(context.Request["id"], out id)) 
        {
            id = 0;
        }

        var connectionString = ConfigurationManager.ConnectionStrings["some_db"].ConnectionString;
        using (var connection = new SqlConnection(connectionString))
        using (var command = connection.CreateCommand())
        {
            connection.Open();
            command.CommandText = "select image from some_table where image_id = :id";
            command.Parameters.AddWithValue("id", id);
            using (var reader = command.ExecuteReader())
            {
                if (reader.Read()) 
                {
                    context.Response.ContentType = "application/pdf";
                    var cd = new ContentDisposition();
                    cd.FileName = "test.pdf";
                    cd.Inline = true;
                    context.Response.AddHeader("Content-Disposition", cd.ToString());

                    long bytesRead;
                    int size = 1024;
                    var buffer = new byte[size];
                    long dataIndex = 0;
                    while ((bytesRead = reader.GetBytes(0, dataIndex, buffer, 0, buffer.Length)) > 0)
                    {
                        var actual = new byte[bytesRead];
                        Buffer.BlockCopy(buffer, 0, actual, 0, (int)bytesRead);
                        context.Response.OutputStream.Write(actual, 0, actual.Length);
                        dataIndex += bytesRead;
                    }
                }
                else
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Not found");
                    context.Response.StatusCode = 404;
                }
            }
        }
    }

    public bool IsReusable
    {
        get { return false; }
    }
}
在aspx页面中,只需放置引用此处理程序的锚:

<a href="/PdfHandler.ashx?id=1">pdf 1</a>
<a href="/PdfHandler.ashx?id=2">pdf 2</a>
<a href="/PdfHandler.ashx?id=3">pdf 3</a>
...

恐怕您需要更详细地了解PDF文件如何存储在数据库中,然后才能在这里得到合理的答案。我们在这里是陌生人,所以我们不知道您的应用程序是如何工作的。您使用的是什么数据库SQL Server??PDF文件如何存储在数据库中?链接按钮是如何创建的?恐怕你需要更具体地说明PDF文件是如何存储在数据库中的,然后才能在这里得到合理的答案。我们在这里是陌生人,所以我们不知道您的应用程序是如何工作的。您使用的是什么数据库SQL Server??PDF文件如何存储在数据库中?链接按钮是如何创建的?