如何从gridview下载文件 [英] how to download file from gridview

查看:97
本文介绍了如何从gridview下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 protected void DownloadFile(object sender,EventArgs e)
{
string filePath =(sender as LinkBut​​ton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader(Content-Disposition,attachment; filename =+ Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}





使用此代码我可以下载从我的系统上传的文件,我无法下载另一个系统中的同一个文件...........做什么。它显示未找到异常文件路径。

解决方案

见下面的链接有关下载代码的备用详细信息: -

http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html [ ^ ]

http://gridviewdownload.blogspot.in/ [ ^ ]

http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkBut​​ton-Click-Event-in-ASPNet-using-C-and-VBNet .aspx [ ^ ]


在数据库中保存文件名



  protected   void  GridView1_RowCommand( object  sender,GridViewCommandEventArgs e)
{
if (e.CommandName == download
{
Session [ sid] = e.CommandArgument.ToString();
if (sconn.State == ConnectionState.Open)
{
sconn.Close();
}
sconn.Open();
string qry = 从File_Table中选择文件夹其中Id =' + Session [ sid]。ToString()+ < span class =code-string> ';
SqlCommand cmd = new SqlCommand(qry,sconn);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);

string fName = dt.Rows [ 0 ] [ 0 ]的ToString();

cmd.ExecuteNonQuery();
sconn.Close();
if (fName ==
{
ScriptManager.RegisterStartupScript( this this .GetType (), message alert('No Download Link Found'); true );
}
else
{
Response.Write( < script> alert(' + fName + ')< /脚本> 中);
Response.ContentType = application / octet-stream;
Response.AddHeader( Content-Disposition attachment; filename = + fName);
Response.TransmitFile(Server.MapPath( 〜/ Files / + fName)) ;
Response.End();
}
}
}


请参阅我的提示 - 从ASP.NET 4.0中的GridView行下载文件 [ ^ ]。

protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}



by using this code iam able to download the file which is uploaded from my system and iam not able to download the same file in another system ...........what to do.it is showing exception file path not found.

解决方案

see below links for alternate details of download code:-
http://www.aspdotnet-suresh.com/2012/02/saveupload-files-in-folder-and-download.html[^]
http://gridviewdownload.blogspot.in/[^]
http://www.aspsnippets.com/Articles/Download-Files-from-GridView-using-LinkButton-Click-Event-in-ASPNet-using-C-and-VBNet.aspx[^]


Save File Name in the Database

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "download")
        {
            Session["sid"] = e.CommandArgument.ToString();
            if (sconn.State == ConnectionState.Open)
            {
                sconn.Close();
            }
            sconn.Open();
            string qry = "Select  FIle from File_Table where Id='" + Session["sid"].ToString() + "' ";
            SqlCommand cmd = new SqlCommand(qry, sconn);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);

            string fName = dt.Rows[0][0].ToString();

            cmd.ExecuteNonQuery();
            sconn.Close();
            if (fName == "")
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('No Download Link Found');", true);
            }
            else
            {
                Response.Write("<script>alert('" + fName + "')</script>");
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fName);
                Response.TransmitFile(Server.MapPath("~/Files/" + fName));
                Response.End();
            }
        }
    }


Refer my Tip - File download from GridView rows in ASP.NET 4.0[^].


这篇关于如何从gridview下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆