_Fc 2019-05-07 22:50 采纳率: 0%
浏览 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条回答

  • threenewbee 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()是不是没有值

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图