首页 新闻 会员 周边 捐助

asp.net 文件下载

0
悬赏园豆:50 [已解决问题] 解决于 2011-03-04 17:30

我在GridView中,显示出download文件夹下的文件,然后在GridView中添加一个下载按钮,怎么实现点击下载后,弹出一个对话框,提醒用户保存该文件...

[小伍]的主页 [小伍] | 初学一级 | 园豆:60
提问于:2011-03-03 15:09
< >
分享
最佳答案
0

<asp:LinkButton ID="upload" runat="server" Text="下载"  OnClick="upload_Click" />

 

int rowIndex = 0;
        LinkButton btn = (LinkButton)sender;
        GridViewRow gvr = (GridViewRow)btn.Parent.Parent;
        rowIndex = gvr.RowIndex;
        string mID = GridView1.Rows[rowIndex].Cells[0].Text.Trim();
        string sqlStr = "select * from SFPrintPaper where ID='" mID "' ";
        dt = Utils.executeQueryT(sqlStr, connStr);
        if (dt.Rows.Count > 0)
        {
            String FullFileName = System.Web.HttpContext.Current.Server.MapPath("../../BarTender/" dt.Rows[0]["FileName"].ToString().Trim() ".btw");
            FileInfo DownloadFile = new FileInfo(FullFileName);
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.HeaderEncoding = Encoding.Default;
            Response.AddHeader("Content-Disposition", " attachment;filename=" System.Web.HttpUtility.UrlEncode("Label.btw", System.Text.Encoding.UTF8).Replace(" ", ""));
            Response.ContentType = "application/octet-stream";
            Response.ContentEncoding = Encoding.Default;
            Response.WriteFile(DownloadFile.FullName);
            Response.Flush();
            Response.OutputStream.Close();
            Response.End();
        }

收获园豆:45
陳修君 | 菜鸟二级 |园豆:480 | 2011-03-04 16:53
其他回答(3)
0

<a href='filePath' target='_blank'>下载</a>

写代码的小2B | 园豆:4377 (老鸟四级) | 2011-03-03 17:42
0

可以使用javascript控制,如果想简单使用一个js库,如jquery

2012 | 园豆:21645 (高人七级) | 2011-03-03 21:54
0

 简单的做法是 <a> 标签添加文件路径

  复杂点的话,如果你想加权限控制,如下:

 

Response.ContentType = "application/x-zip-compressed";

Response.AddHeader(
"Content-Disposition", "attachment;filename=" fileurl "");
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

string filename = Server.MapPath("UpFile/" model.fileurl);
Response.TransmitFile(filename);
收获园豆:5
clound | 园豆:481 (菜鸟二级) | 2011-03-04 13:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册