1

I have this function that works correctly as is. When called - it creates a file into my browser's download folder:

private void gvLots_Export(string fileName)
{
    Response.Clear();

    Response.AppendHeader("content-disposition", "attachment; filename=" + fileName );
    Response.ContentType = "text/plain";


    Response.Write(fichierExport.Contenu);
    Response.End();

}

But now, I need to refresh the page just after that file is correctly added to the download folder. I have tried this:

    Response.AppendHeader("content-disposition", "attachment; filename=" + fileName );
    Response.ContentType = "text/plain";

    Response.Write(fichierExport.Contenu);
    Response.End();
    Response.Redirect(Request.RawUrl);

However, it does not work for obvious reasons: the header is already sent, you can't redirect after Response.End().

I tried to add a variable into the header that could be retrieved by Resquest["stop"] (as seen below):

Response.AppendHeader("content-disposition", "?stop=true&" + "attachment; filename=" + fichierExport.NomFichier );

But these results are even worse. It does not refresh the page or download anything, but it shows me the content of the file I'm trying to download into the browser... is it even possible to download something and refresh the page in the same function... without having to ask the client to push 2 different buttons?

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • 1
    The first thing I notice is that your header will look like `content-disposition: ?stop=trueattachment; filename=somefile.ext` – ahwm May 19 '15 at 17:14

2 Answers2

1

Since I can't comment yet, I will post this here.

Look at: Refresh Page C# ASP.NET. Have you tried doing it the way described in the link? e.g.:

Response.Redirect(Request.Url.ToString(), true);

As described here, the RawUrl only contains the part of the URL after the domain information and the Url contains everything.

Community
  • 1
  • 1
Cory
  • 783
  • 5
  • 12
  • 1
    Thanks for your effort Cory, but here is the reel problem, if i write your line BEFORE the response.End(), my header is completely ignored and no file is downloaded, if i write it AFTER the response.End(), it raise an exeption and shut down the program...gave you points so you can comment, it's weird not having enough reputation to just comment, but enough to answer – Antoine Pelletier May 19 '15 at 17:44
1

I assume what i'm asking for cannot be done... I didn't see anything like what i'm trying to do on any internet page. I'll make another hyperLink that just refresh the page and i'll ask all the customers to click on it after they download...

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62