_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 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题