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 });
// 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
ah yes, the elusive "an" error
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
to me it doesn't make that much sense what you are doing
downloading a file is a thing to itself
yeah your response is either a file or a redirect, can't be both