C
C#14mo ago
Kurwaii

C# redirect after sending file through headers

Using the code below I am getting an error, can't redirect after setting headers, is there any work around so that I can redirect to the page after the user downloads the file?
// Prepare the file content byte[] fileBytes = excelPackage.GetAsByteArray(); // Disable buffering to send the headers immediately Response.BufferOutput = false; // Set the response headers for the file download Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Disposition", "attachment; filename=CredenciaisPrestadores.xlsx"); // Send the file content without flushing Response.BinaryWrite(fileBytes); // Perform any other actions before the redirect here // Now, enable buffering and flush the response to initiate the file download Response.BufferOutput = true; Response.Flush(); // Redirect after the response has been sent return RedirectToAction("Details", new { Id = id });
4 Replies
Jimmacle
Jimmacle14mo ago
ah yes, the elusive "an" error
Kurwaii
KurwaiiOP14mo ago
The error is "Can't redirect after setting headers" sorry if it wasn't clear Managed to fix by running the download through a different window and then redirecting back to the old one
FestivalDelGelato
to me it doesn't make that much sense what you are doing downloading a file is a thing to itself
Jimmacle
Jimmacle14mo ago
yeah your response is either a file or a redirect, can't be both

Did you find this page helpful?