Force download ASP.Net

2019-06-21 19:55发布

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.

2条回答
可以哭但决不认输i
2楼-- · 2019-06-21 20:12

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

查看更多
小情绪 Triste *
3楼-- · 2019-06-21 20:19
                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();
查看更多
登录 后发表回答