C# 图像不是通过点击按钮下载的,即放置在网格内

C# 图像不是通过点击按钮下载的,即放置在网格内,c#,asp.net,C#,Asp.net,我无法通过单击按钮下载图像。。我将按钮放在网格内,以下载相应的图像。。下面是我的代码 protected void DownloadFile(object sender, EventArgs e) { int id = int.Parse((sender as LinkButton).CommandArgument); byte[] bytes; string fileName, contentType; string constr = Configuration

我无法通过单击按钮下载图像。。我将按钮放在网格内,以下载相应的图像。。下面是我的代码

protected void DownloadFile(object sender, EventArgs e)
{
    int id = int.Parse((sender as LinkButton).CommandArgument);
    byte[] bytes;
    string fileName, contentType;
    string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=" + id;
            cmd.Parameters.AddWithValue("@Id", id);
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                sdr.Read();
                bytes = (byte[])sdr["FileContent"];
                contentType = sdr["Extentions"].ToString();
                fileName = sdr["FileName"].ToString();
            }
            con.Close();
        }
    }
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = contentType;
    Response.AddHeader("Content-type", contentType);
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

命令文本更改为:

cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=@Id";
读者读到:

 while(sdr.Read())
 {
                bytes = (byte[])sdr["FileContent"];
                contentType = sdr["Extentions"].ToString();
                fileName = sdr["FileName"].ToString();
 }
编辑:为了测试,我使用以下代码从磁盘而不是数据库获取文件:

protected void DownloadFile(object sender, EventArgs e)
{
    int id = int.Parse((sender as LinkButton).CommandArgument);
    byte[] bytes;
    string fileName, contentType;

    // Test code  to download 123.sql file from C:\
    bytes = System.IO.File.ReadAllBytes("C:\\123.sql");
    contentType = ".sql";
    fileName = "123.sql";
    //string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
    //using (SqlConnection con = new SqlConnection(constr))
    //{
    //    using (SqlCommand cmd = new SqlCommand())
    //    {
    //        cmd.CommandText = "select FileName,Extentions, FileContent from Documents where ID=" + id;
    //        cmd.Parameters.AddWithValue("@Id", id);
    //        cmd.Connection = con;
    //        con.Open();
    //        using (SqlDataReader sdr = cmd.ExecuteReader())
    //        {
    //            sdr.Read();
    //            bytes = (byte[])sdr["FileContent"];
    //            contentType = sdr["Extentions"].ToString();
    //            fileName = sdr["FileName"].ToString();
    //        }
    //        con.Close();
    //    }
    //}
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = contentType;
    Response.AddHeader("Content-type", contentType);
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

编辑2:是我使用的测试项目的链接。

在Fiddler中请求/响应是什么样子的?它怎么不起作用?我们需要更多的诊断信息。代码从头到尾都在成功运行。。它没有给出任何错误。但是映像也没有下载。您必须进行调试。在
Response.BinaryWrite(字节)处放置断点
并查看字节的值。如果为null(我怀疑是这样),则在
Response.BinaryWrite(字节)处设置断点
,获取sql,将@Id替换为Id值并在sssms中运行查询。我已将您的数据库读取部分替换为
bytes=File.ReadAllBytes(“C:\\123.sql”)并且它正在从我的磁盘下载123.sql文件。
字节的大小是多少?它太大了吗?它的尺寸是3059。我添加了测试代码。您可以尝试使用物理文件。如果它对您有效,那么sql数据有问题。您可以下载我的测试项目的副本。有一个备份的数据库,我已用于测试。它工作得很好!