moluo_14 2017-03-11 10:21 采纳率: 0%
浏览 900

ASP.net文件下载遇到的问题

做项目遇到的问题,大神帮忙看看啊!要交工了,来不及了!!

文件显示没有问题,也没有语法错误,点击下载就是弹不出来保存的页面
大神帮帮忙啊!!

预览如下:
图片说明

我的代码如下:

 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();

        }
    }

}
  • 写回答

1条回答 默认 最新

  • Go 旅城通票 2017-03-11 13:04
    关注

    有转向附带dFile参数没有?html代码的属性值要用引号括起

    
    td.Controls.Add(new LiteralControl("<a href=\"?dFile=" + Server.UrlEncode(fsi.Name) + "\">下载</a>"));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制