如何指定用户通过 ASHX 下载器下载的文件的名称?

发布于 2024-12-08 02:48:19 字数 631 浏览 0 评论 0原文

这是我第一次编写允许用户下载其他用户上传的文件的代码。

我编写了一个 ASHX 文件 download.ashx,其代码如下所示:

s = context.Request.QueryString.ToString();
byte[] buffer = new ReplacementTicketFileIO().GetSpecifiedFile(s);
context.Response.BinaryWrite(buffer);
context.Response.Flush();
context.Response.End();

当用户使用适当的查询字符串单击 download.ashx 的链接时,该文件将被下载,但浏览器希望在浏览器窗口。如果用户右键单击该链接,则可以下载该文件,但文件名称默认为 download.ashx。

我想完成两件事:

1)我希望能够根据查询字符串指定在用户设备上下载的文件的默认名称。

例如,如果用户单击 download.ashx?linkedfile=car.pdf,我希望浏览器默认使用 car.pdf 作为该文件的名称。

2)我希望浏览器默认保存链接,而不是在浏览器窗口中打开链接。

我这样做是否合理,或者有更好的方法来下载文件?请告诉我。

This is my first time writing code that allows a user to download a file uploaded by another user.

I've written an ASHX file, download.ashx, with code that looks like this:

s = context.Request.QueryString.ToString();
byte[] buffer = new ReplacementTicketFileIO().GetSpecifiedFile(s);
context.Response.BinaryWrite(buffer);
context.Response.Flush();
context.Response.End();

When a user clicks on a link to download.ashx with the appropriate querystring, the file is downloaded, but the browser wants to display the content in the browser window. If the user right-clicks on the link, he can download the file, but the name of the file defaults to download.ashx.

I would like to accomplish two things:

1) I would like to be able to specify the default name of the file downloaded on the user's device based on the querystring.

For instance, if the user clicks on download.ashx?linkedfile=car.pdf, I would like for the browser to default to car.pdf for the name of this file.

2) I would like for the browser to default to saving the link, as opposed to opening the link in the browser window.

Is it reasonable for me to want to do this, or is there a better way to download files? Please let me know.

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

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

发布评论

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

评论(2

榆西 2024-12-15 02:48:19

设置 Content-Disposition HTTP 标头。例如,

Content-Disposition: attachment; filename=hello.jpg

您可以在 C# 中使用以下命令来做到这一点:

 Response.AddHeader("Content-Disposition", "attachment; filename=hello.jpg");

Set the Content-Disposition HTTP header. E.g.

Content-Disposition: attachment; filename=hello.jpg

You can do that in C# using:

 Response.AddHeader("Content-Disposition", "attachment; filename=hello.jpg");
难忘№最初的完美 2024-12-15 02:48:19

这是我为 Excel 文件准备的东西,我相信它会强制下载而不是打开新窗口。 QueryString 有一个页面属性。您只需要捕获 QueryString 并在此代码中使用它以及确定内容类型。 String.Format 将为您提供干净的代码。

private string _ExcelFilename
{
        get
{
    return (Request.QueryString["xls"] != null) ? Request.QueryString"xls"] : "bis";
}
}

Page.Response.Clear();
Page.EnableViewState = false;
Page.Response.Clear();
Page.Response.ContentType = "application/vnd.ms-excel";
Page.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}_{1}.xls", _ExcelFilename, DateTime.Now.ToString("yyyyMMdd")));
Page.Response.Write(excel);
Page.Response.Flush();
Page.Response.End();

Here is something I have for excel files and I believe it forces a download rather than a new window. There is a page property for QueryString. You would just need to capture the QueryString and use it in this code as well as determining the content type. The String.Format will give you clean code.

private string _ExcelFilename
{
        get
{
    return (Request.QueryString["xls"] != null) ? Request.QueryString"xls"] : "bis";
}
}

Page.Response.Clear();
Page.EnableViewState = false;
Page.Response.Clear();
Page.Response.ContentType = "application/vnd.ms-excel";
Page.Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}_{1}.xls", _ExcelFilename, DateTime.Now.ToString("yyyyMMdd")));
Page.Response.Write(excel);
Page.Response.Flush();
Page.Response.End();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文