首先这是我的aspx文件代码
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Don.aspx.cs" Inherits="Don" Title="无标题页" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style2
        {
            width: 771px;
            height: 100px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <table class="style2">
        <tr>
            <td align="center">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#3366CC" DataKeyNames="did"
                    BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="540px" 
                    onrowcreated="GridView1_RowCreated">
        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
        <RowStyle BackColor="White" ForeColor="#003399" />
        <Columns>
            <asp:BoundField DataField="dname" HeaderText="文件名称" SortExpression="dname" />
            
            <asp:TemplateField HeaderText="文件下载">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/下载.gif" 
                        onclick="ImageButton1_Click1" />
                </ItemTemplate>
            </asp:TemplateField>
            
            
            <asp:BoundField DataField="did" HeaderText="did" SortExpression="did" 
                Visible="False" />
            <asp:BoundField DataField="dherf" SortExpression="dherf" />
           
            <asp:HyperLinkField DataNavigateUrlFields="did" 
                DataNavigateUrlFormatString="dhref" DataTextField="dw" FooterText="1" 
                HeaderText="文件下载2" />
                
        </Columns>
        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    </asp:GridView>
            </td>
        </tr>
    </table>
&nbsp;
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:cycpsConnectionString %>" 
        SelectCommand="SELECT * FROM [don]"></asp:SqlDataSource>
        
</asp:Content>其次再是CS文件代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
public partial class Don : System.Web.UI.Page
{
    
 protected void Page_Load(object sender, EventArgs e)
    {    }
    
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {    }
    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {
        SqlConnection myConn = new SqlConnection("server=(local); database=cycps; uid=sa; pwd=;");
        myConn.Open();
        //获取imgbtnDelete的ImageButton对象
        ImageButton imgbtn = (ImageButton)sender;
        //引用imgbtnDelete控件的父控件上一级控件
        GridViewRow gvr = (GridViewRow)imgbtn.Parent.Parent;
        //获取文件真实姓名
        string sqlStr = "select dname from [don] where did='" + GridView1.DataKeys[gvr.RowIndex].Value.ToString() + "'";
        //打开数据库
        SqlDataAdapter dapt = new SqlDataAdapter(sqlStr, myConn);
        DataSet ds = new DataSet();
        dapt.Fill(ds, "files");
        //获取文件路径
        string strFilePath = Server.MapPath("Don//" + ds.Tables["don"].Rows[0][0].ToString());
        ds.Dispose();
        myConn.Close();
        
        if (File.Exists(strFilePath))
        {
            System.IO.FileInfo file = new System.IO.FileInfo(strFilePath);
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = true;
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name));
            Response.AppendHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();
            Response.End();    }}    protected void imgbtnDF_Click(object sender, ImageClickEventArgs e)
    {
        
        }
    }最终是错误信息
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 41:         DataSet ds = new DataSet();
行 42:         //获取文件路径
行 43:         string strFilePath = Server.MapPath("Don//" + ds.Tables["don"].Rows[0][0].ToString());
行 44:         ds.Dispose();
行 45:         myConn.Close();
 源文件: d:\工作区\系统开发-张帅\财院操盘手\实验室\Don.aspx.cs    行: 43 请问该如何修改??

解决方案 »

  1.   

    我的数据库很简单 表Don  did是主键,还有dname就是文件的名字。要下载的文件放在当前目录下Don文件夹下。
      

  2.   

    if(ds!=null&&ds.Tables["don"].Rows.Count>0)
    {
    string strFilePath = Server.MapPath("Don//" + ds.Tables["don"].Rows[0][0].ToString());
    }
      

  3.   

    网站根目录下的Don目录下的,这样写,正斜杠,不用写两个的,反斜杠需要用2个,一个表示转义的。"~/Don/" + ds.Tables["don"].Rows[0][0].ToString()还有你首先输出一下这个试试看,就是该路径是否确实存在这个文件,如果没有的话,肯定找不着对象了,就会报错。Reponse.Write("~/Don/" + ds.Tables["don"].Rows[0][0].ToString());
      

  4.   


    在Don文件夹下面有个文件 叫 1 我把这句改成
    string strFilePath = Server.MapPath("Don//" + 1);后再点击下载按钮后没有反映,请问我这样做测试是否能执行下载可行么?
      

  5.   

    你需要下载具体某个文件,Don/1 这个地址的话,你下载什么呢?
    得有个具体的文件如 Don/1/test.txt等
      

  6.   

    try{}
    catch(Exception ex){}
    点 断点 自己查一下,试试