© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
5 replies
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 });
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Sending a file through Sockets C# TCP missing chunks
C#CC# / help
14mo ago
Executing a jar file through c#
C#CC# / help
16mo ago
Running .jar file through c# on windows?
C#CC# / help
16mo ago
Modifying the hosts file through c# code
C#CC# / help
2y ago