有没有可能以编程方式保存到指定下载文件夹之外的另一个文件夹?

发布于 2024-12-14 06:41:41 字数 1437 浏览 4 评论 0原文

我正在开发一个 ASP.NET Web 应用程序,其中将表转换为 Excel 电子表格。我想为用户提供将其保存到下载部分以外的其他位置的选项。我意识到这很可能是被禁止的,但也许 ASP.net 框架促进了某种类/机制。

这是我当前的代码:

protected void saveDataButton_Click(Object sender, EventArgs e)
    {
        SaveFileDialog browser = new SaveFileDialog();
        string fileName;

        if (browser.ShowDialog() == DialogResult.OK)
        {
            fileName = browser.FileName;
        }
        else
            return;

       DataTable table = (DataTable)Session["tableData"];


       HttpContext context = HttpContext.Current;
       context.Response.Clear();

       context.Response.ContentType = "text/csv";
       context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");



       foreach (DataColumn column in table.Columns)
       {
            context.Response.Write(column.ColumnName + ";");
       }
       context.Response.Write(Environment.NewLine);
       foreach (DataRow row in table.Rows)
       {
            for (int i = 0; i < row.ItemArray.Length; i++)
            {
                context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
            }
            context.Response.Write(Environment.NewLine);
       }

       context.Response.End();
    }

我知道在这种情况下我使用 fileName 的方式是错误的,因为 fileName 实际上在我的代码中存储文件路径+文件名。有什么办法可以指定文件路径吗?

我能想到的唯一其他解决方案是,如果我在页面上创建某种文件并让它们右键单击+另存为。这是一个糟糕的选择吗?还有其他人吗?

I'm developing a ASP.NET Web application in which a table is converted to an excel spreadsheet. I would like to give the option to the user to save somewhere else than the downloads section. I realize that is a good likelihood that this is prohibited, but maybe there is some sort of class/mechanism facilitated by ASP.net framework.

Here is my current code:

protected void saveDataButton_Click(Object sender, EventArgs e)
    {
        SaveFileDialog browser = new SaveFileDialog();
        string fileName;

        if (browser.ShowDialog() == DialogResult.OK)
        {
            fileName = browser.FileName;
        }
        else
            return;

       DataTable table = (DataTable)Session["tableData"];


       HttpContext context = HttpContext.Current;
       context.Response.Clear();

       context.Response.ContentType = "text/csv";
       context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");



       foreach (DataColumn column in table.Columns)
       {
            context.Response.Write(column.ColumnName + ";");
       }
       context.Response.Write(Environment.NewLine);
       foreach (DataRow row in table.Rows)
       {
            for (int i = 0; i < row.ItemArray.Length; i++)
            {
                context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
            }
            context.Response.Write(Environment.NewLine);
       }

       context.Response.End();
    }

I know the way I'm using fileName is wrong in this context, as fileName is actually storing file path + file name in my code. Any way to specify a file path?

The only other solution I can think of is if I created the some sort of file on the page and had them right click + save as. Is this a bad alternative? Are there others?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御弟哥哥 2024-12-21 06:41:41

不。Web 应用程序对本地文件系统一无所知。 (事实上​​,除了 content-disposition 标头之外,HTTP 协议甚至不知道“文件”是什么。)

您正在做什么(除了 content-disposition 标头中包含完整路径之外 ) code>content-disposition 标头)是正确的。将文件发送到 Web 客户端的标准方法是设置该标头并将文件内容写入输出。客户端发生的事情完全取决于网络浏览器。如果用户的 Web 浏览器设置为自动将下载内容保存到特定文件夹而不提示,则您无法从服务器更改该设置。

No. The web application doesn't know anything about the local file system. (Indeed, aside from the content-disposition header, the HTTP protocol doesn't even really know what a "file" is.)

What you're doing (aside from having the full path in the content-disposition header) is correct. The standard way to send a file to a web client is to set that header and write the file content to the output. What happens on the client-side is entirely up to the web browser. If the user's web browser is set to automatically save downloads to a specific folder without prompting then you can't change that from the server.

好倦 2024-12-21 06:41:41

不,文件保存在用户计算机上的位置是由网络浏览器决定的,与网络服务器无关。想想看,您不知道站点访问者正在运行什么操作系统,更不用说您在服务器上指定的任意路径了。

no, where the file is saved on the user's computer is dictated by the web browser, it has nothing to do with the web server. think about it, you don't know what operating system a site visitor is running, let alone some arbitrary path you specify on the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文