Downloading csv files with zip?
I have this codes, the problem is, whenever I press the download button, it gives an error indicating Directory Not Found. I have already an upload function with the following fileUpload.PostedFile.SaveAs(Server.MapPath("~/Upload"));
Below is my codes:
protected void Page_Load(object s开发者_如何学编程ender, 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);
}
}
Can anyone help me with this please? When I use Directory.GetFiles(@"~/Upload"), I get the mentioned error
Directory.GetFiles expects an existing local path, die ~ Syntax is not possible here. Use
MapPath
before:
var files = Directory.GetFiles(Request.MapPath("~/Upload"));
精彩评论