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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)