首页 新闻 会员 周边

ASP.NET文件下载

0
悬赏园豆:5 [已解决问题] 解决于 2013-04-25 17:02

这些是从数据库读出来的附件路径,请问如何只显示文件名,还有就是在点击的时候能能下载到本地?

问题补充:

我现在用的是BulletedList控件,ListView控件也试了下,但是没有思路!!!

实习研究僧的主页 实习研究僧 | 初学一级 | 园豆:12
提问于:2013-04-25 14:22
< >
分享
最佳答案
0

1:IO.Path.GetFileName(path)

2:点击时,读取成byte[],然后输出即可,参考代码:

/// <summary>
        /// 输出文件
        /// </summary>
        /// <param name="filePath">文件所在路径</param>
        private void OutPutFile(string filePath, string outPutName)
        {

            Byte[] fileData = DownFileFromUrlPath(filePath);
            if (fileData == null)
            {
                HttpContext.Current.Response.Write("file not exist!!");
                HttpContext.Current.Response.End();
            }

            try
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                // HttpContext.Current.Response.BufferOutput = false;
                HttpContext.Current.Response.ContentType = "application/force-download";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(outPutName, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AddHeader("Content-Length", fileData.Length.ToString());
                HttpContext.Current.Response.BinaryWrite(fileData);
                HttpContext.Current.Response.Flush();
            }
            catch
            {

            }
            finally
            {
                fileData = null;
            }
            HttpContext.Current.Response.End();
        }
收获园豆:5
路过秋天 | 老鸟四级 |园豆:4787 | 2013-04-25 14:32
其他回答(1)
0

楼上正解

丫的 | 园豆:1575 (小虾三级) | 2013-04-25 15:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册