首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

文件上载不了啊错哪了啊

2012-12-26 
文件下载不了啊,啊,啊,啊,,,,哪里错了啊,啊,啊,啊,,,,求救啊,,protected void btnDown_Click(object sende

文件下载不了啊,啊,啊,啊,,,,哪里错了啊,啊,啊,啊,,,,求救啊,,
protected void btnDown_Click(object sender, EventArgs e)
        {
string d_name="123.pdf";
            string s_path = HttpContext.Current.Server.MapPath("/userfiles/downfiles/" + d_name);
            if (System.IO.File.Exists(s_path))
            {
                HttpContext.Current.Response.ContentType = "application/ms-download";
                System.IO.FileInfo file = new System.IO.FileInfo(s_path);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
                HttpContext.Current.Response.WriteFile(file.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.End();
            }
            else
            {
                Response.Write("<script>alert('下载失败,下载源提取失败!');history.go(-1);</script>");
                return;
            }
        }

123.pdf的相对路径:网站根目录/userfiles/downfiles/123.pdf

在本地测试没问题,但放到服务器上,总是说下载失败,下载源提取失败,找不到这个123.pdf文件!

请大侠帮忙,,,,这个问题该咋整啊?

急啊
[最优解释]
调试,或者日志输出下
 (System.IO.File.Exists(s_path))
看看s_path是不是不对,或者没有权限。
[其他解释]
路径问题了
[其他解释]
MapPath("/userfiles/downfiles/" + d_name);
换成
MapPath("~/userfiles/downfiles/" + d_name);
[其他解释]
    string s_path = HttpContext.Current.Server.MapPath("/userfiles/downfiles/" + d_name);


换成    string s_path = Server.MapPath("~/userfiles/downfiles/" + d_name);
[其他解释]
你看下 服务器存放 文件的那个文件夹的有没有读写文件的权限
[其他解释]

引用:
回楼上的,我在地址栏中直接输入完整的地址加文件名(如:http://www.xxx.com/userfiles/downfiles/123.pdf)可以下载,为什么用上面的方法不行呢,,,,那个文件夹没有权限的话,在地址栏中直接输入也不应该可以下载啊


那很有可能是你的获取的文件路径没对,你把s_path获取的值打印出来看看。

[其他解释]
自己顶一下(晕,,要不少于六个字......)
[其他解释]
引用:
调试,或者日志输出下
 (System.IO.File.Exists(s_path))
看看s_path是不是不对,或者没有权限。


有没权限什么意思啊?
[其他解释]
4楼,5楼的兄弟,我都试了,还是不行啊
[其他解释]
回楼上的,我在地址栏中直接输入完整的地址加文件名(如:http://www.xxx.com/userfiles/downfiles/123.pdf)可以下载,为什么用上面的方法不行呢,,,,那个文件夹没有权限的话,在地址栏中直接输入也不应该可以下载啊
[其他解释]
我晕。。。。原来是没有获取到文件名,,,,
但是我在page_load时将值给一个public的变量了,为什么在另一个linkbutton的click中获取不到这个值啊?在本地能获取到,传到服务器上就获取不到,,,,为什么啊,,不应该啊
[其他解释]
无语啊,,,原来是这个鸟问题啊,,搞定了,谢谢大家

热点排行