做项目遇到的问题,大神帮忙看看啊!要交工了,来不及了!!
文件显示没有问题,也没有语法错误,点击下载就是弹不出来保存的页面
大神帮帮忙啊!!
预览如下:
我的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class index : System.Web.UI.Page
{
string root;
Table tableDirInfo = new Table();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["dfile"] != null && Request.QueryString["dfile"] != "")
{
//要下载的文件名称
string downFile = Request.QueryString["dFile".ToString()];
DownLoadFile(downFile);
}
root = Server.MapPath("~/publicfiles");
GetFileList(root);
Panel1.Controls.Add(tableDirInfo);
}
}
private void GetFileList(string strCurDir)
{
string FileName, FileExt;
long FileSize;
if (Directory.Exists(Path.GetDirectoryName(strCurDir)))
{
FileInfo fi;
DirectoryInfo dir;
TableCell td;
TableHeaderCell th;
TableRow tr;
tr = new TableRow();
th = new TableHeaderCell();
th.Controls.Add(new LiteralControl("文件名"));
tr.Cells.Add(th);
th = new TableHeaderCell();
th.Controls.Add(new LiteralControl("文件类型"));
tr.Cells.Add(th);
th = new TableHeaderCell();
th.Controls.Add(new LiteralControl("文件大小"));
tr.Cells.Add(th);
tableDirInfo.Rows.Add(tr);
//根据当前目录建立目录引用对象
DirectoryInfo dirInfo = new DirectoryInfo(strCurDir);
//循环判断当前目录下的文件和目录
foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
{
FileName = "";
FileExt = "";
FileSize = 0;
if (fsi is FileInfo)//如果是文件
{
fi = (FileInfo)fsi;
FileName = fi.Name;
FileExt = fi.Extension;
FileSize = fi.Length;
}
else//否则是目录
{
dir = (DirectoryInfo)fsi;
FileName = dir.Name;
FileExt = "文件夹";
//获取文件夹路径
GetFileList(strCurDir + "\\" + dir.Name);
}//#else
//动态添加表格内容
tr = new TableRow();
td = new TableCell();
td.Controls.Add(new LiteralControl(FileName));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl(FileExt));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl(FileSize.ToString() + "字节"));
tr.Cells.Add(td);
td = new TableCell();
td.Controls.Add(new LiteralControl("<a href=?dFile=" + Server.UrlEncode(fsi.Name) + ">下载</a>"));
//string dFile = Server.UrlEncode(fsi.Name);
tr.Cells.Add(td);
tableDirInfo.Rows.Add(tr);
}
}
}
private void DownLoadFile(string fileName)
{
string filePath = Server.MapPath("~/publicfiles") + fileName;
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
}
}
}