Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# 从SQLServer2000下载二进制文件_C#_Sql Server_.net 3.5_Binaryfiles - Fatal编程技术网

C# 从SQLServer2000下载二进制文件

C# 从SQLServer2000下载二进制文件,c#,sql-server,.net-3.5,binaryfiles,C#,Sql Server,.net 3.5,Binaryfiles,我插入了二进制文件(图像、PDF、视频…),我想检索此文件以下载它 我使用了通用处理程序页面,如下所示 public void ProcessRequest (HttpContext context) { using (System.Data.SqlClient.SqlConnection con = Connection.GetConnection()) { String Sql = "Select BinaryData From ProductsDownloa

我插入了二进制文件(图像、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>


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

虽然您可以解决您的问题来缩小范围,但我猜您的浏览器没有将其识别为要下载的文件,而是尝试显示它

这是因为你只需要发送字节,你就应该告诉浏览器从服务器返回了什么数据。方法是设置内容标题

// 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”]
结果进行类型转换以获得字符串,但这要留给实现者。

到底是什么问题?
SqlCommand
SqlDataReader
都需要使用
blocks.ps在
bytes=Encoding.UTF8.GetBytes(String.Empty)似乎有点没用?谢谢你的兴趣,它真的很好,但与图像,winrar没有pdf或视频。但当我试图插入pdf、视频或大文件时,数据库不接受这一点。注意。我在SQL2000上工作,好吧,那么你是说在DB中添加文件不起作用?为什么不另提一个问题,因为上传文件是一个完全不同的问题。很抱歉,下载效果很好,但我如何才能在数据库上上传大尺寸,因为当我上传大尺寸不起作用时,你是二进制的,COLLM的最大大小是4000字节,所以大于3.9kb是一个问题,只需使用
varbinary即可(max)
你会没事的:)