强制下载 ASP.Net

发布于 2025-01-06 00:56:29 字数 1536 浏览 3 评论 0原文

在 ASP.Net(使用 C#)中,我尝试创建一个包含纯文本的 .DAT 文件并将其发送到浏览器并强制下载。我已经尝试了几件事,但我无法让它发挥作用。在我的 aspx 文件中,有一个 ImageButton

<asp:ImageButton ID="btnSave" runat="server" CausesValidation="False" ImageUrl="~/Images/Stages/Database/Save.png" OnClick="btnSave_OnClick" Width="26px" />

在 OnClick 方法中,我尝试创建该文件并将其发送到浏览器。

protected void btnSave_OnClick(object sender, EventArgs e)
{
    string file = "test.dat";
    string fileName = "~\\Stages\\Broekx\\Databanken\\" + file;

    FileStream fs = new FileStream(MapPath(fileName), FileMode.Open);
    long cntBytes = new FileInfo(MapPath(fileName)).Length;
    byte[] byteArray = new byte[Convert.ToInt32(cntBytes)];
    fs.Read(byteArray, 0, Convert.ToInt32(cntBytes));
    fs.Close();

    ImageButton btnSave = (ImageButton)FormViewStagesDummy.FindControl("btnSave");
    btnSave.Visible = false;

    File.Delete(Server.MapPath(fileName));

    if (byteArray != null)
    {
        this.Response.Clear();
        this.Response.ContentType = "text/plain";
        this.Response.AddHeader("content-disposition", "attachment;filename=" + file);
        this.Response.BinaryWrite(byteArray);
        this.Response.End();
        this.Response.Flush();
        this.Response.Close();
    }
}

文件 test.dat 存在于正确的文件夹中,在读入字节后必须将其删除。我已经在不删除文件的情况下尝试过此操作,但这也不起作用。

单击 btnSave 后,按钮必须隐藏,所以这就是我将参数 Visible 设置为 false 的原因。

我还尝试过使用内容类型“application/octet-stream”或使用 PDF 文件和内容类型“application/pdf”,但没有任何效果。页面加载正常,没有文件正在下载。

In ASP.Net (with C#) I'm trying to create a .DAT file with plain text in it and send it to the browser and force download. I've tried several things but I can't get it working. In my aspx-file there is an ImageButton

<asp:ImageButton ID="btnSave" runat="server" CausesValidation="False" ImageUrl="~/Images/Stages/Database/Save.png" OnClick="btnSave_OnClick" Width="26px" />

In the OnClick-method I'm trying to create the file and send it to the browser.

protected void btnSave_OnClick(object sender, EventArgs e)
{
    string file = "test.dat";
    string fileName = "~\\Stages\\Broekx\\Databanken\\" + file;

    FileStream fs = new FileStream(MapPath(fileName), FileMode.Open);
    long cntBytes = new FileInfo(MapPath(fileName)).Length;
    byte[] byteArray = new byte[Convert.ToInt32(cntBytes)];
    fs.Read(byteArray, 0, Convert.ToInt32(cntBytes));
    fs.Close();

    ImageButton btnSave = (ImageButton)FormViewStagesDummy.FindControl("btnSave");
    btnSave.Visible = false;

    File.Delete(Server.MapPath(fileName));

    if (byteArray != null)
    {
        this.Response.Clear();
        this.Response.ContentType = "text/plain";
        this.Response.AddHeader("content-disposition", "attachment;filename=" + file);
        this.Response.BinaryWrite(byteArray);
        this.Response.End();
        this.Response.Flush();
        this.Response.Close();
    }
}

The file test.dat exists in the correct folder and has to be deleted after it has been read into bytes. I've tried this without deleting the file and that wont work either.

After clicking btnSave the button has to be hidden, so that's why I set the parameter Visible to false.

I've also tried it with content-type "application/octet-stream" or with a PDF file and content-type "application/pdf" but nothing works. The page loads normally and no file is being downloaded.

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

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

发布评论

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

评论(2

总攻大人 2025-01-13 00:56:29

文件字符串的路径真的正确吗?

this.Response.AddHeader("content-disposition", "attachment;filename=" + file);

不应该是文件名吗?

为什么要在将文件写入响应之前删除该文件?通过响应提供文件然后删除它不是更有意义吗?

调用

File.Delete(Server.MapPath(fileName));

即在响应后

。您应该尝试:

Response.TransmitFile( Server.MapPath(fileName) );
Response.End();

TransmitFile 非常高效,因为它基本上将文件流卸载到 IIS,包括可能导致文件缓存在内核缓存中(基于 IIS 的缓存规则)。
响应.End();

Is the file string's path actually correct?

this.Response.AddHeader("content-disposition", "attachment;filename=" + file);

Should it not be filename?

Why are you deleting the file before it is written to the response? Would it not make more sense to serve the file via the response and then delete it?

i.e. call

File.Delete(Server.MapPath(fileName));

after the repsonse.

You should try:

Response.TransmitFile( Server.MapPath(fileName) );
Response.End();

TransmitFile is very efficient because it basically offloads the file streaming to IIS including potentially causing the file to get cached in the Kernal cache (based on IIS's caching rules).
Response.End();

茶花眉 2025-01-13 00:56:29
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", "attachment; filename = " + fileName);
                Response.TransmitFile(Server.MapPath("~/foldername/" + fileName));
                Response.End();
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", "attachment; filename = " + fileName);
                Response.TransmitFile(Server.MapPath("~/foldername/" + fileName));
                Response.End();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文