从 SQL Server 2000 下载二进制文件

发布于 2024-08-25 21:19:36 字数 3341 浏览 4 评论 0原文

我插入了二进制文件(图像、PDF、视频..),我想检索该文件以下载它。

我使用通用处理程序页面,

public void ProcessRequest (HttpContext context) {
    using (System.Data.SqlClient.SqlConnection con = Connection.GetConnection())
    {
        String Sql = "Select BinaryData From ProductsDownload Where Product_Id = @Product_Id";

        SqlCommand com = new SqlCommand(Sql, con);
        com.CommandType = System.Data.CommandType.Text;

        com.Parameters.Add(Parameter.NewInt("@Product_Id", context.Request.QueryString["Product_Id"].ToString()));

        SqlDataReader dr = com.ExecuteReader();

        if (dr.Read() && dr != null)
        {
            Byte[] bytes;
            bytes = Encoding.UTF8.GetBytes(String.Empty);
            bytes = (Byte[])dr["BinaryData"];
            context.Response.BinaryWrite(bytes);

            dr.Close();
        }
    }
}

这是我的表格

CREATE TABLE [ProductsDownload] (
 [ID] [bigint] IDENTITY (1, 1) NOT NULL ,
 [Product_Id] [int] NULL ,
 [Type_Id] [int] NULL ,
 [Name] [nvarchar] (200) COLLATE Arabic_CI_AS NULL ,
 [MIME] [varchar] (50) COLLATE Arabic_CI_AS NULL ,
 [BinaryData] [varbinary] (4000) NULL ,
 [Description] [nvarchar] (500) COLLATE Arabic_CI_AS NULL ,
 [Add_Date] [datetime] NULL ,
 CONSTRAINT [PK_ProductsDownload] PRIMARY KEY  CLUSTERED 
 (
  [ID]
 )  ON [PRIMARY] ,
 CONSTRAINT [FK_ProductsDownload_DownloadTypes] FOREIGN KEY 
 (
  [Type_Id]
 ) REFERENCES [DownloadTypes] (
  [ID]
 ) ON DELETE CASCADE  ON UPDATE CASCADE ,
 CONSTRAINT [FK_ProductsDownload_Product] FOREIGN KEY 
 (
  [Product_Id]
 ) REFERENCES [Product] (
  [Product_Id]
 ) ON DELETE CASCADE  ON UPDATE CASCADE 
) ON [PRIMARY]
GO

,并且使用数据列表具有文件名标签和下载文件的按钮,因为

<asp:DataList ID="DataList5" runat="server" 
              DataSource='<%#GetData(Convert.ToString(Eval("Product_Id")))%>'
              RepeatColumns="1" RepeatLayout="Flow">
     <ItemTemplate>
         <table width="100%" border="0" cellspacing="0" cellpadding="0">
             <tr>
                 <td class="spc_tab_hed_bg spc_hed_txt lm5 tm2 bm3">
                     <asp:Label ID="LblType" runat="server" Text='<%# Eval("TypeName", "{0}") %>'></asp:Label>
                 </td>
                 <td width="380" class="spc_tab_hed_bg">
                     &nbsp;
                 </td>
             </tr>
             <tr>
                 <td align="left" class="lm5 tm2 bm3">
                     <asp:Label ID="LblData" runat="server" Text='<%# Eval("Name", "{0}") %>'></asp:Label>
                 </td>
                 <td align="center" class=" tm2 bm3">
                     <a href='<%# "DownloadFile.aspx?Product_Id=" + DataBinder.Eval(Container.DataItem,"Product_Id") %>' >
                       <img src="images/downloads_ht.jpg" width="11" height="11" border="0" />
                     </a>
                     <%--<asp:ImageButton ID="ImageButton1" ImageUrl="images/downloads_ht.jpg" runat="server" OnClick="ImageButton1_Click1" />--%>
                  </td>
              </tr>
          </table>
      </ItemTemplate>
 </asp:DataList>

我尝试了更多方法来解决这个问题,但我不能。

I inserted binary files (images, PDF, videos..) and I want to retrieve this file to download it.

I used generic handler page as this

public void ProcessRequest (HttpContext context) {
    using (System.Data.SqlClient.SqlConnection con = Connection.GetConnection())
    {
        String Sql = "Select BinaryData From ProductsDownload Where Product_Id = @Product_Id";

        SqlCommand com = new SqlCommand(Sql, con);
        com.CommandType = System.Data.CommandType.Text;

        com.Parameters.Add(Parameter.NewInt("@Product_Id", context.Request.QueryString["Product_Id"].ToString()));

        SqlDataReader dr = com.ExecuteReader();

        if (dr.Read() && dr != null)
        {
            Byte[] bytes;
            bytes = Encoding.UTF8.GetBytes(String.Empty);
            bytes = (Byte[])dr["BinaryData"];
            context.Response.BinaryWrite(bytes);

            dr.Close();
        }
    }
}

and this is my table

CREATE TABLE [ProductsDownload] (
 [ID] [bigint] IDENTITY (1, 1) NOT NULL ,
 [Product_Id] [int] NULL ,
 [Type_Id] [int] NULL ,
 [Name] [nvarchar] (200) COLLATE Arabic_CI_AS NULL ,
 [MIME] [varchar] (50) COLLATE Arabic_CI_AS NULL ,
 [BinaryData] [varbinary] (4000) NULL ,
 [Description] [nvarchar] (500) COLLATE Arabic_CI_AS NULL ,
 [Add_Date] [datetime] NULL ,
 CONSTRAINT [PK_ProductsDownload] PRIMARY KEY  CLUSTERED 
 (
  [ID]
 )  ON [PRIMARY] ,
 CONSTRAINT [FK_ProductsDownload_DownloadTypes] FOREIGN KEY 
 (
  [Type_Id]
 ) REFERENCES [DownloadTypes] (
  [ID]
 ) ON DELETE CASCADE  ON UPDATE CASCADE ,
 CONSTRAINT [FK_ProductsDownload_Product] FOREIGN KEY 
 (
  [Product_Id]
 ) REFERENCES [Product] (
  [Product_Id]
 ) ON DELETE CASCADE  ON UPDATE CASCADE 
) ON [PRIMARY]
GO

And use data list has label for file name and button to download file as

<asp:DataList ID="DataList5" runat="server" 
              DataSource='<%#GetData(Convert.ToString(Eval("Product_Id")))%>'
              RepeatColumns="1" RepeatLayout="Flow">
     <ItemTemplate>
         <table width="100%" border="0" cellspacing="0" cellpadding="0">
             <tr>
                 <td class="spc_tab_hed_bg spc_hed_txt lm5 tm2 bm3">
                     <asp:Label ID="LblType" runat="server" Text='<%# Eval("TypeName", "{0}") %>'></asp:Label>
                 </td>
                 <td width="380" class="spc_tab_hed_bg">
                      
                 </td>
             </tr>
             <tr>
                 <td align="left" class="lm5 tm2 bm3">
                     <asp:Label ID="LblData" runat="server" Text='<%# Eval("Name", "{0}") %>'></asp:Label>
                 </td>
                 <td align="center" class=" tm2 bm3">
                     <a href='<%# "DownloadFile.aspx?Product_Id=" + DataBinder.Eval(Container.DataItem,"Product_Id") %>' >
                       <img src="images/downloads_ht.jpg" width="11" height="11" border="0" />
                     </a>
                     <%--<asp:ImageButton ID="ImageButton1" ImageUrl="images/downloads_ht.jpg" runat="server" OnClick="ImageButton1_Click1" />--%>
                  </td>
              </tr>
          </table>
      </ItemTemplate>
 </asp:DataList>

I tried more to solve this problem but I cannot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-09-01 21:19:37

尽管您可以解决您的问题以缩小范围,但我猜您的浏览器不会将其识别为要下载的文件,而不是尝试显示它?

这是因为您只是发送字节,因此您应该告诉浏览器从服务器返回的数据是什么。做到这一点的方法是设置内容标题。

// optionally provide the filename instead of 
// letting the browser create one based on the get url
context.Response.AddHeader("content-disposition", "attachement filename=" + dr["Name"]);
// set the MIME content type of the data.
context.Response.ContentType = dr["MIME"];

我不记得您是否需要对 dr["MIME"] 结果进行类型转换以获取字符串,但这留给了实现者。

Although you could work on your question to narrow it down, I'm guessing your browser does not recognize it as a file to download instead of trying to show it?

It's because your just sending bytes as a result, you should tell the browser what data is coming back from the server. The way to do that is to set the content headers.

// optionally provide the filename instead of 
// letting the browser create one based on the get url
context.Response.AddHeader("content-disposition", "attachement filename=" + dr["Name"]);
// set the MIME content type of the data.
context.Response.ContentType = dr["MIME"];

I don't remember if you need to do a typecast on the dr["MIME"] result to get a String, but that's left for the implementer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文