-1

我有一个启用了文件流的 SQL Server 表。我可以在这里上传文件并进行更新。如何将这些文件读入 .aspx 网页?文件就是.pdf文件。

每行有 2 个文件关联。

我正在将该行读入 .aspx 页面,并且在底部我希望文件在另一个下方打开。这是为了让用户打印文件所在的行。

我已经检查了@dinglemeyer 建议的代码。我确实想将我的pdf转换为图像。我假设有一个简单的方法:

   <asp:GridView ID="gridview_f" runat="server" ShowHeaderWhenEmpty="false" PageSize="2" DataKeyNames="ID" DataSourceID="sql_files" AutoGenerateColumns="false" >
       <Columns>
           <asp:TemplateField Visible="false">
               <ItemTemplate>
                   <asp:Label ID="selID" runat="server" Text='<%# Bind("ID") %>' />
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField>
               <ItemTemplate>
                   <asp:Label ID="selFname" runat="server" Text='<%# Bind("filename") %>' />
                   <br />
                   <asp:image ID="selFile" runat="server" ImageUrl='<%# "Handler.ashx?id="+Eval("ID") %>' />
               </ItemTemplate>
           </asp:TemplateField>
       </Columns>
   </asp:GridView>


   // code behind for ginfing the grid as the parentId is extracted from other form values.
    private void gridView_Data(string id)
           {
               sql_files.SelectCommand = "SELECT ID, filename from myFILESTABLE where PARENTID=" + id;
    }


    // Handler.ascx
          public void ProcessRequest(HttpContext context)
        {

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader sdr = null;

            conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["my_dbconn"].ConnectionString);
            try
            {
                    cmd = new SqlCommand("SELECT filecontent from myFILESTABLE WHERE ID=" + context.Request.QueryString["ID"], conn);
                conn.Open();
                sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    context.Response.ContentType = "content/pdf";
                    context.Response.BinaryWrite((byte[])sdr["filecontent"]);
                }
                sdr.Close();
            }
            finally
            {
                conn.Close();
            }
4

1 回答 1

0

对于延迟回复,我深表歉意。来自链接: http ://weblogs.asp.net/aghausman/saving-and-retrieving-file-using-filestream-sql-server-2008这是我的工作代码:

   <asp:GridView ID="mygrid1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" DataSourceID="sql_file" OnRowCommand="gridview_f_RowCommand"  GridLines="None" >
       <Columns>
           <asp:TemplateField HeaderText="S.No">
               <ItemTemplate>
                   <asp:Label ID="Label12" runat="server" Text='<%#  Container.DataItemIndex + 1 %>'  />
               </ItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Attachments" >
               <ItemTemplate>
                   <asp:Label ID="Label11" runat="server" Text='<%# Bind("ID") %>' Visible="false" />
                   <asp:LinkButton ID="lLabel12" runat="server" Text='<%# Bind("myFileName") %>' CommandName="GetFile" CommandArgument='<%#Eval("ID") + "|" + Eval("myFileName") %>'>                           
                   </asp:LinkButton>
               </ItemTemplate>
           </asp:TemplateField>
       </Columns>
   </asp:GridView>

" >

后面的代码:

     protected void gridview_f_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="GetFile")
        {
            string[] ca = e.CommandArgument.ToString().Split('|');
            SqlConnection objSqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);
            objSqlCon.Open();
            SqlTransaction objSqlTran = objSqlCon.BeginTransaction();

            SqlCommand objSqlCmd = new SqlCommand("spGet_getMyFile", objSqlCon, objSqlTran);
            objSqlCmd.CommandType = CommandType.StoredProcedure;

            SqlParameter objSqlParam1 = new SqlParameter("@ID", SqlDbType.VarChar);
            objSqlParam1.Value = ca[0]; // e.CommandArgument;

            objSqlCmd.Parameters.Add(objSqlParam1);

            string path = string.Empty;
            using (SqlDataReader sdr = objSqlCmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    path = sdr[0].ToString();
                }
            }

            objSqlCmd = new SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", objSqlCon, objSqlTran);

            byte[] objContext = (byte[])objSqlCmd.ExecuteScalar();

            SqlFileStream objSqlFileStream = new SqlFileStream(path, objContext, FileAccess.Read);

            byte[] buffer = new byte[(int)objSqlFileStream.Length];
            objSqlFileStream.Read(buffer, 0, buffer.Length);
            objSqlFileStream.Close();
            objSqlTran.Commit();
            Response.AddHeader("Content-disposition", "attachment; filename=" + ca[1]);
            Response.ContentType = "application/pdf";

            Response.BinaryWrite(buffer);


        }
    }

存储过程 SpGet_getMyfile 是:

创建过程 [dbo].[spGet_getMyFile] (@id BIGINT)

AS

SELECT   filestreamColumn.PathName()
   FROM dbo.TableThatHasTheFileStream
      WHERE Id=@id
于 2016-01-19T17:10:34.303 回答