用zip下载csv文件?

时间:2011-08-29 07:39:35

标签: c# asp.net csv

我有这些代码,问题是,每当我按下下载按钮时,它都会显示错误,指示找不到目录。我已经有了以下fileUpload.PostedFile.SaveAs(Server.MapPath("~/Upload"));的上传功能。以下是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var files = Directory.GetFiles(@"~/Upload");
        gvFiles.DataSource = from f in files
                             select new
                             {
                                 FileName = Path.GetFileName(f)
                             };
        gvFiles.DataBind();
    }

}

protected void btnDownload_Click(object sender, EventArgs e)
{

    string fileName = string.Empty;
    string filepath = Request.MapPath("~/Upload");
    string downloadFileName = "Attendance.zip";
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "filename=" + downloadFileName);

    using (ZipFile zip = new ZipFile())
    {
        foreach (GridView row in gvFiles.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkSelect");
            if (cb != null && cb.Checked)
            {
                fileName = (row.FindControl("lblFileName") as Label).Text;
                zip.AddFile(Server.MapPath(Path.Combine(filepath, fileName)), "");
            }
        }
        zip.Save(Response.OutputStream);
    }
}

有人可以帮我吗?当我使用Directory.GetFiles(@“〜/ Upload”)时,我得到了上面提到的错误

1 个答案:

答案 0 :(得分:1)

enter image description here Directory.GetFiles需要一个现有的本地路径,这里不能使用语法〜语法。之前使用MapPath

var files = Directory.GetFiles(Request.MapPath("~/Upload"));
相关问题