2010-03-23 36 views
0

我插入二進制文件(圖像,PDF,視頻..),我想檢索這個文件來下載它。從SQL Server 2000下載二進制文件

我使用的通用處理程序頁面,因爲這

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> 

我試着解決這個問題,但我不能。

+0

究竟是什麼**是問題? – 2010-03-23 15:51:07

+1

你的'SqlCommand'和'SqlDataReader'都需要在'使用'塊中。 – 2010-03-23 15:55:58

+1

ps。 'bytes = Encoding.UTF8.GetBytes(String.Empty);'似乎有點無用? – 2010-03-23 16:02:09

回答

1

雖然你可以在你的問題上努力縮小範圍,但我猜你的瀏覽器不會將它識別爲下載文件而不是試圖顯示它?

這是因爲你只是發送字節作爲結果,你應該告訴瀏覽器什麼數據從服務器回來。要做到這一點的方法是設置內容標題。

// 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"]結果類型轉換得到一個字符串,但那是留給實施者我不記得了。

+0

感謝您的興趣,它真的很好,但與圖像,winrar沒有pdf或視頻。 但是當我試圖插入pdf,視頻或大文件數據庫不接受這一點。 注意。我工作在sql2000 – Myworld 2010-03-24 13:20:47

+0

好吧,所以你說在數據庫中添加文件不起作用?爲什麼不把這個問題作爲一個單獨的問題,因爲文件的上傳是另外一個問題。 – 2010-03-24 13:52:59

+0

我很抱歉處理下載運行良好,但我怎麼能 上傳數據庫上的大尺寸 因爲當我上傳大尺寸不起作用 – Myworld 2010-04-01 15:18:54