weixin_43447253
_Fc
采纳率0%
2019-05-07 22:50 阅读 343

asp.NET 我想对数据库中的文件下载下来 大佬们帮我看看为什么会这样 跪求!交作业了

图片说明

我的文件是以二进制形式上传到数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class chakan : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

     if (!this.IsPostBack)
    {
        this.databindTodg();
    }

}

private void databindTodg()

{

    SqlConnection con = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\TS.mdf;User Instance=true");        
    con.Open();
    string sql = @"select * from Documents ";//string sql = "select * from Documents";        
    SqlDataAdapter sda = new SqlDataAdapter(sql, con);        
    DataSet ds = new DataSet();        
    sda.Fill(ds);        
    this.GridView1.DataSource = ds.Tables[0].DefaultView;
    GridView1.DataSourceID = null;
    GridView1.DataSource = ds;
    this.DataBind();    
}
protected void lbtnDownLoad_Command(object sender, CommandEventArgs e)
{
    int SNo = Convert.ToInt32(e.CommandArgument.ToString());
    SqlConnection con = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\TS.mdf;User Instance=true");
    con.Open();
    string sql = "select * from Documents where SNo=" + SNo + "";
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataReader sdr = cmd.ExecuteReader();
    sdr.Read();
    Response.Buffer = true;
    Page.Response.Clear();//清除缓冲区所有内容        
    Page.Response.ContentType = "application/octet-stream";

    Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sdr["Name_File"].ToString()));
    byte[] file = (Byte[])sdr["fileData"];//读出数据         
    int a = file.Length;        
    Response.BinaryWrite(file);        
    Response.Flush();        
    Response.End();        
    sdr.Close();        
    con.Close();   

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • caozhy 从今以后生命中的每一秒都属于我爱的人 2019-05-08 16:06

    string fn = HttpUtility.UrlEncode(sdr["Name_File"].ToString());
    Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn);
    先这样写,看错误发生在上面一行还是下面一行
    如果是上面一行,看sdr["Name_File"].ToString()是不是没有值

    点赞 评论 复制链接分享

相关推荐