C# SQL Server 2008文件流读取到网页
我有一个启用了filestream的SQL Server表。我可以在这里上传文件并进行更新。如何将这些文件读入.aspx网页?这些文件是C# SQL Server 2008文件流读取到网页,c#,asp.net,sql-server-2008,sqlfilestream,C#,Asp.net,Sql Server 2008,Sqlfilestream,我有一个启用了filestream的SQL Server表。我可以在这里上传文件并进行更新。如何将这些文件读入.aspx网页?这些文件是.pdf文件 每行关联2个文件 我正在将该行读入一个.aspx页面,在底部,我希望文件一个接一个地打开。这供用户打印包含文件的行 我已经检查了@dinglemeyer建议的代码。我确实想将我的pdf转换为图像。我假设有一个简单的方法: <asp:GridView ID="gridview_f" runat="server" ShowHeaderWhe
.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();
}
//用于从其他表单值提取parentId时对网格进行ginfing的代码隐藏。
私有void gridView_数据(字符串id)
{
sql_files.SelectCommand=“从myFILESTABLE中选择ID,文件名,其中PARENTID=“+ID;
}
//Handler.ascx
公共void ProcessRequest(HttpContext上下文)
{
SqlConnection-conn=null;
SqlCommand cmd=null;
SqlDataReader sdr=null;
conn=new-SqlConnection(System.Configuration.ConfigurationManager.ConnectionString[“my_dbconn”].ConnectionString);
尝试
{
cmd=new-SqlCommand(“从myFILESTABLE中选择filecontent,其中ID=“+context.Request.QueryString[“ID”],conn”);
conn.Open();
sdr=cmd.ExecuteReader();
while(sdr.Read())
{
context.Response.ContentType=“content/pdf”;
context.Response.BinaryWrite((字节[])sdr[“文件内容”]);
}
sdr.Close();
}
最后
{
康涅狄格州关闭();
}
对于延迟回复,我深表歉意。通过链接:这是我的工作代码:
<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>
存储过程SpGet_getMyfile是:
创建过程[dbo]。[spGet\U getMyFile]
(
@id BIGINT
)
如果您发布您尝试过的代码的相关部分,这个问题会更好。我能够编写/更新文件,但无法以pdf格式读取。我的SQL是:从tblFiles中选择fileContent,其中ID=?我在TSql中获取数据。我可以拥有一个gridview,其中fileContent作为数据,但无法填充它。我的意思是您应该添加一个代码块,通过在
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);
}
}
AS
SELECT filestreamColumn.PathName()
FROM dbo.TableThatHasTheFileStream
WHERE Id=@id