当我使用带数据库的ASP

当我使用带数据库的ASP

本文介绍了当我使用带数据库的ASP.NET单击下载链接时打开pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是文件已下载但未以新形式打开。





我有什么试过:



 protected void DownloadFile(object sender,EventArgs e)
{
// string filePath =(发送者为LinkBut​​ton).CommandArgument;
int id = int.Parse((发送者为LinkBut​​ton).CommandArgument);
byte []字节;
string fileName,contentType;
string constr = ConfigurationManager.ConnectionStrings [conn]。ConnectionString;
using(SqlConnection con = new SqlConnection(constr))
{
using(SqlCommand cmd = new SqlCommand())
{
cmd.CommandText =select name ,FileName,ContentType,来自tblFiles的数据,其中Id = @ Id;
cmd.Parameters.AddWithValue(@ Id,id);
cmd.Connection = con;
con.Open();
using(SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes =(byte [])sdr [Data];
contentType = sdr [ContentType]。ToString();
fileName = sdr [Name]。ToString();
}
con.Close();
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset =;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =application / pdf;
Response.AppendHeader(Content-Disposition,attachment; filename =+ fileName);

//Response.ContentType = ContentType;
//Response.AppendHeader(\"Content-Disposition,attachment; filename =+ Path.GetFileName(filePath));
//Response.WriteFile(filePath);
//Response.End();

Response.BinaryWrite(bytes);
Response.End();

// {
// String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings [conn]。ConnectionString;
// LinkBut​​ton lnkbtn =发送者为LinkBut​​ton;
// GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
// int fileid = Convert.ToInt32(GrvImageFiles.DataKeys [gvrow.RowIndex] .Value.ToString());
// //字符串名称,类型;
// using(SqlConnection con = new SqlConnection(strConnString))
// {
// using(SqlCommand cmd = new SqlCommand())
// {
// cmd.CommandText =选择名称,FileName,ContentType,来自tblFiles的数据,其中Id = @Id;
// cmd.Parameters.AddWithValue(@ id,fileid);
// cmd.Connection = con;
// con.Open();
// SqlDataReader dr = cmd.ExecuteReader();
// if(dr.Read())
// {
// Response.ContentType = dr [FileType]。ToString();
// Response.AddHeader(Content-Disposition,attachment; filename = \+ dr [FileName] +\);
// Response.BinaryWrite((byte [])dr [FileData]);
// Response.End();
//}
//}
//}

// 2。 // string filePath =(发送者为LinkBut​​ton).CommandArgument;
//Response.ContentType = ContentType;
//Response.AppendHeader(\"Content-Disposition,attachment; filename =+ Path.GetFileName(filePath));
//Response.WriteFile(filePath);
//Response.End();


}
解决方案



problem is file is downloaded but not open in new form.



What I have tried:

protected void DownloadFile(object sender, EventArgs e)
        {
          //  string filePath = (sender as LinkButton).CommandArgument;
            int id = int.Parse((sender as LinkButton).CommandArgument);
            byte[] bytes;
            string fileName, contentType;
            string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select Name,FileName,ContentType,Data from tblFiles where Id=@Id";
                    cmd.Parameters.AddWithValue("@Id", id);
                    cmd.Connection = con;
                    con.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        sdr.Read();
                        bytes = (byte[])sdr["Data"];
                        contentType = sdr["ContentType"].ToString();
                        fileName = sdr["Name"].ToString();
                    }
                    con.Close();
                }
            }
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);

            //Response.ContentType = ContentType;
            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            //Response.WriteFile(filePath);
            //Response.End();

            Response.BinaryWrite(bytes);            
            Response.End();

        //{
        //    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        //    LinkButton lnkbtn = sender as LinkButton;
        //    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        //    int fileid = Convert.ToInt32(GrvImageFiles.DataKeys[gvrow.RowIndex].Value.ToString());
        //  //  string name, type;
        //    using (SqlConnection con = new SqlConnection(strConnString))
        //    {
        //        using (SqlCommand cmd = new SqlCommand())
        //        {
        //            cmd.CommandText = "select Name,FileName, ContentType,Data from tblFiles where Id=@Id";
        //            cmd.Parameters.AddWithValue("@id", fileid);
        //            cmd.Connection = con;
        //            con.Open();
        //            SqlDataReader dr = cmd.ExecuteReader();
        //            if (dr.Read())
        //            {
        //                Response.ContentType = dr["FileType"].ToString();
        //                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["FileName"] + "\"");
        //                Response.BinaryWrite((byte[])dr["FileData"]);
        //                Response.End();
        //            }
        //        }
        //    }

     //2.     //string filePath = (sender as LinkButton).CommandArgument;
            //Response.ContentType = ContentType;
            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            //Response.WriteFile(filePath);
            //Response.End();


        }
解决方案



这篇关于当我使用带数据库的ASP.NET单击下载链接时打开pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:00