【问题标题】:ASP .Net C# - Get File location after downloadedASP .Net C# - 下载后获取文件位置
【发布时间】:2015-02-13 10:13:46
【问题描述】:

我目前正在开发一个简单的程序(使用 ASP.Net C#)将数据从 GridView 填充到 Excel 文件中。需要将 Excel 文件下载到客户端计算机中。

由于某些原因,我需要在 Excel 文件下载到客户端本地计算机后快速对其进行操作。

问题是我无法获取下载文件的位置。 下载到客户端计算机后如何获取文件位置?

这是截图:

下载代码:

private void Create_ExcelContent2()
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=" + ddlBatchID.Text + ".xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw); ...
        gvBatchSummary.RenderControl(hw);
        string style = @"<style> .textmode { } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}

【问题讨论】:

  • 显示您用于下载文件的代码
  • 私人无效 Create_ExcelContent2() { Response.Clear();响应缓冲区=真; Response.AddHeader("content-disposition", "attachment;filename=" + ddlBatchID.Text + ".xls"); Response.Charset = ""; Response.ContentType = "应用程序/vnd.ms-excel";使用 (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); ... gvBatchSummary.RenderControl(hw);字符串样式 = @""; Response.Write(风格); Response.Output.Write(sw.ToString()); Response.Flush();响应。结束(); } }
  • 在“Respone.Flush()”行执行后显示保存对话框。
  • 编辑您的问题(并格式化),不要在 cmets 中添加代码。
  • @fubo 我只需要地址。获得地址后,我将使用它来获取文件(因为我已经有了地址),打开文件并将其内容复制到一个新的空 Excel 文件中。它。

标签: c# asp.net file location


【解决方案1】:

对此的简短回答是你不能这样做。一旦文件在本地机器上,服务器端代码就不能用来操作它。如果可以的话,安全隐患将是一个雷区。

【讨论】:

    【解决方案2】:

    你为什么不试试下面的方法

    string folderPath = string.Empty;
    
    using (FolderBrowserDialog fdb = new FolderBrowserDialog()) {
      if (fdb.ShowDialog() == DialogResult.OK ){
        folderPath = fdb.SelectedPath;
      }
    }
    

    抱歉我没看到@fubo,

    编辑:

    如果你想要那个目录路径,那你为什么不把它保存到一个带前缀的本地系统路径中,然后你可以从那里读取和操作它。

    【讨论】:

    • 让我试试。谢谢@jithendra
    • FolderBrowserDialog 是 WinForms - 他谈到了 ASP.Net
    • 查看编辑后的答案,您可以使用提升的权限仪式访问文件,然后我认为这是可能的,我没有尝试过抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2012-06-26
    • 1970-01-01
    • 2017-08-02
    • 2017-04-28
    • 2020-07-21
    • 1970-01-01
    相关资源
    最近更新 更多