C#C
C#3y 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 });
Was this page helpful?