【发布时间】:2014-07-02 10:17:01
【问题描述】:
我有一个 GridView,其中包含数据,包括文件名,我只能从一个文件夹下载文件,而无法下载子文件夹中的文件。我一直在用这个
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("Upload\\Track\\Files") + e.CommandArgument);
Response.End();
}
}
我已经尝试添加这个,但我收到一个错误,说明它是非法的。
string path = Server.MapPath("\\Upload\\Track\\Files\\ ,*," + SearchOption.AllDirectories);
Response.TransmitFile(path + e.CommandArgument);
我希望能够使用此方法下载所有文件,包括子文件夹中的文件。
已更新——我也尝试过这种方式,但没有成功,我知道我可以找到正确的路径,但就是无法下载。
string path = Server.MapPath("\\Upload\\Track\\Files");
string filename = e.CommandArgument.ToString();
DirectoryInfo dir = new DirectoryInfo(path);
string pathString = System.IO.Path.Combine(dir + path);
if (e.CommandName == "Download")
{
System.IO.Directory.GetDirectories(path);
if (File.Exists(pathString)) {
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "filename=" + filename);
Response.TransmitFile(pathString);
Response.End();
【问题讨论】:
-
stackoverflow.com/questions/5907465/…>
-
我不想一次下载所有文件。我的 GridView 中的某些文件链接无法下载,因为文件的路径位于不同的子文件夹中
-
e.CommandArgument 是否包含,只是文件名或相对路径+文件名?
-
只包含文件名
-
那是问题所在,您还应该将相对子文件夹路径绑定到CommandArgument