_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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)