强制下载ASP.Net [英] Force download ASP.Net

查看:92
本文介绍了强制下载ASP.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.Net(用C#)我试图创建一个有明文.DAT文件,并将其发送给浏览器和下载力。我试过几件事情,但我无法得到它的工作。在我的aspx文件有一个的ImageButton

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" />

在onclick法我试图创建文件,并将其发送给浏览器。

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();
    }
}

TEST.DAT存在于正确的文件夹,并有它已经被读入字节后删除该文件。我试过这个,但不删除文件,并且不会工作,无论是。

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.

点击btnSave按钮后已被隐藏,所以这就是为什么我设置的参数,可见为false。

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

我也与内容类型应用程序/八位字节流,或者一个PDF文件和内容类型application / PDF但没有任何工程试了一下。在页面加载正常,没有文件正在下载。

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.

推荐答案

是文件字符串的路径实际上是正确的?

Is the file string's path actually correct?

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

岂不是文件名?

你为什么要删除文件,然后写入到响应?岂不更有意义通过响应服务文件,然后删除呢?

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?

即。呼叫

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

在repsonse后。

after the repsonse.

您应该尝试:

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

的TransmitFile是非常有效的,因为它基本上卸载文件串流到IIS,包括可能导致在籽粒缓存获取缓存文件(基于IIS的缓存规则)。
    到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();

这篇关于强制下载ASP.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆