读书人

文件下载不了啊错哪了啊

发布时间: 2013-03-26 09:54:34 作者: rapoo

文件下载不了啊,啊,啊,啊,,,,哪里错了啊,啊,啊,啊,,,,求救啊,,
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获取的值打印出来看看。

读书人网 >asp.net

热点推荐