【问题标题】:GridView download files in SubfoldersGridView 下载子文件夹中的文件
【发布时间】: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

标签: c# asp.net gridview


【解决方案1】:

所以我找到了问题的解决方案,我在数据库中添加了一个字段,其中包含有关路径的信息 - 我从上传文件时获取此路径,它捕获路径并将其添加到数据库中。我还在 GridView 的上传字段中添加了以下代码。

这个解决方案目前最适合我,但当然可以改进。

<asp:TemplateField HeaderText="Quote" InsertVisible="false">
<ItemTemplate>
<a href="<%# Eval("FilePath") %>/<%# Eval("Uploads") %>"><%# Eval("Uploads") %></a>
</ItemTemplate>
</asp:TemplateField>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    相关资源
    最近更新 更多