我的数据表为MYtable   
里面的数据如下(我把文件以二进制形式存到数据表里了):   
Id(序号)                       int   
files(文件内容)               image   
title(文件标题)               varchar   
Type(文件类型)                   varchar   
FileDx(文件大小)           varchar   
比如我存的一个文件就是   
Id                               files                                                       title                               Type                                                               FileDx           
1   <Binary>   业务流程                               .doc                                       296448   我想实现用gridview控件绑定数据比如绑定成   
序号                           文件标题                               文件大小   
1                           业务流程                               296448   
2                            .....                             .....   
3                            .....                              ....   
当点击gridview中的"文件标题"列时就能在本页跳出一个对话框可以保存数据库中相应的文件   
请大家指教一下,弄了半天也没出来。
前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="download.aspx.cs" Inherits="download" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="371px"  _disibledevent="GridView1_RowCommand" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:BoundField  DataField="Id" HeaderText="编号" />
                    <asp:BoundField DataField="type" HeaderText="文件类型" />
                    <asp:BoundField DataField="title" HeaderText="文件名称" />
                    <asp:TemplateField HeaderText="下载">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="dnl" CommandArgument="Id">下载</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>    
    </div>
    </form>
</body>
</html>后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;public partial class download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        //下载页面
        //int i = Convert.ToInt32(Request.QueryString["Id"]);
        if (!IsPostBack)
        {
            SqlConnection SqlCon1 = new SqlConnection("server=(local);database=areacount;User Id=HISQUERY;Password=HISQUERY");
            string SqlStr = "select * from myfile";
            SqlDataAdapter SqlSda = new SqlDataAdapter(SqlStr, SqlCon1);
            DataSet Myds = new DataSet();
            SqlCon1.Open(); 
            SqlSda.Fill(Myds, "myfile");
            GridView1.DataSource = Myds;
            GridView1.DataBind();
        }
    }
    private string checktype(string filename)
    { 
     //文件类型判断
        string ContentType;
        switch (filename.ToLower())
        {
         case ".asf":
         ContentType= "video/x-ms-asf";
         break;
        case ".avi":
        ContentType = "video/avi";
        break;
        case ".doc":
        ContentType = "application/msword";
        break;
        case ".zip":
        ContentType = "application/zip";
        break;
        case ".xls":
        ContentType = "application/vnd.ms-excel";
        break;
        case ".gif":
        ContentType = "image/gif";
        break;
        case ".jpg":
        ContentType = "image/jpeg";
        break;
        case "jpeg":
        ContentType = "image/jpeg";
        break;
        case ".wav":
        ContentType = "audio/wav";
        break;
        case ".mp3":
        ContentType = "audio/mpeg3";
        break;
        case ".mpg":
        ContentType = "video/mpeg";
        break;
        case ".mepg":
        ContentType = "video/mpeg";
        break;
        case ".rtf":
        ContentType = "application/rtf";
        break;
        case ".html":
        ContentType = "text/html";
        break;
        case ".htm":
        ContentType = "text/html";
        break;
        case ".txt":
        ContentType = "text/plain";
        break;
        default:
        ContentType = "application/octet-stream";
        break;
        }
        return ContentType;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int i = Convert.ToInt32(e.CommandArgument);
        SqlConnection SqlCon = new SqlConnection("server=(local);database=areacount;User Id=HISQUERY;Password=HISQUERY");
        SqlCon.Open();
        SqlDataAdapter Sda = new SqlDataAdapter("select   *   from   myfile   where   Id=  " + i, SqlCon);
        DataSet Ds = new DataSet();
        Sda.Fill(Ds, "hastable");
        byte[] b2 = (byte[])Ds.Tables["hastable"].Rows[0]["files"];
        string type = (string)Ds.Tables["hastable"].Rows[0]["Type"];
        Response.Clear();
        string Stype =checktype(type);
        Response.AddHeader("Content-Disposition", "attachment;   filename=abc" + type);
        Response.AddHeader("Content-Length", b2.Length.ToString());
        Response.ContentType = Stype;
        Response.BinaryWrite(b2);
        Response.End();
        string FileName = ((LinkButton)sender).CommandArgument;
        Response.Clear();
        Response.ContentType = Stype;
        Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
        Response.WriteFile(FileName);
        Response.End();
    }
}请大家指点一下,不能实现下载

解决方案 »

  1.   

    错误很多,首先:
    int   i   =   Convert.ToInt32(e.CommandArgument); 
    SqlDataAdapter   Sda   =   new   SqlDataAdapter("select       *       from       myfile       where       Id=     "   +   i,   SqlCon); 
     <asp:LinkButton   ID="LinkButton1"   runat="server"   CommandName="dnl"   CommandArgument="Id"> 下载 </asp:LinkButton>
    ///此处是模板列,不能用此种写法.
    数据源数据库语句条件从句,获取对应记录行的相应id值错误.
    详情请见:http://www.itzhe.cn/html/web/ASP.NET/20071215/31376.html
      

  2.   

    Response.AddHeader("Content-Disposition",   "attachment;       filename=abc."   +   type); 
      

  3.   

    string   FileName   =   ((LinkButton)sender).CommandArgument;
    跟前台绑定不符.可用commandname绑定并判断.