文件下载不了啊,啊,啊,啊,,,,哪里错了啊,啊,啊,啊,,,,求救啊,,
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);