单击滚动新闻链接时如何从数据库中检索pdf文件?

问题描述:


单击滚动新闻链接时如何从数据库中检索pdf文件?

Hai
How to retrive a pdf file from database when clicking scrolling news link?

string strsql = "select * from Event";
        string strscrolling = "";
        HtmlTableCell cellscrolling = new HtmlTableCell();
        HtmlTableCell rowScrolling = new HtmlTableCell();
        SqlCommand mycomd = new SqlCommand(strsql, conn);
        SqlDataReader sqlrdr;
        try
        {
            conn.Open();
            sqlrdr = mycomd.ExecuteReader();
            strscrolling="<Marquee onmouseover='this.stop();' onmouseout='this.start();' direction='up'>";
            while (sqlrdr.Read())
            {
                strscrolling = strscrolling + "<a href='#' OnClick=" + "javascript:window.open(target='_blank'?Id=" + sqlrdr.GetValue(0) + "','Ttle','width=400,height=400;toolbar=no;');" + "><font face='verdana' size='2' color='#ffffff'>" + sqlrdr.GetValue(1) + "</a>&nbsp;&nbsp;" + sqlrdr.GetValue(2).ToString() + "</font><br><br>";
            }
            strscrolling=strscrolling+"</Marquee>";
            sqlrdr.Close();
            cellscrolling.InnerHtml=strscrolling;
            //rowScrolling.Cells.Add(cellscrolling);
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);

        }
        finally{
            conn.Close();
        }
            }

如果数据库中有二进制数据,则可以使用HttpHandler沿以下方式吐出数据: />
If you have the binary data in the database you can use an HttpHandler to spit out the data using something along the lines of:
public void ProcessRequest(HttpContext context)
{
    int id;
    if(Int32.TryParse(context.Request.QueryString["id"], out id))
    {
        var data = GetPDFFromDB(id);
        context.Response.Buffer = true;
        context.Response.ContentType = "application/pdf";
        context.Response.AddHeader("Content-Length", data.Length.ToString());
        context.Response.WriteBinary(data);
    }
    else
    {
        context.Response.WriteLine("Invalid id");
    }
}



请参阅文档以了解更多详细信息: http://msdn. microsoft.com/en-us/library/system.web.httpresponse(v=vs.100).aspx [



See the documentation for more details: http://msdn.microsoft.com/en-us/library/system.web.httpresponse(v=vs.100).aspx[^]

Hope this helped :-)


看起来您已经在单击文本字段(PDF名称链接)时打开了一个新窗口.只需在新窗口的Page_Load中打开PDF.看来您已经具有在查询字符串中提取正确的PDF所需的所有详细信息.
Looks like you already are opening a new window on click of a text field (PDF name link). Just open the PDF in Page_Load of new window. Looks like you already have all the needed details for picking up right PDF in querystring.