0

I trying to download SSRS report in excel format from my MVC application.

    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + reportName + "." + extension);
    Response.BinaryWrite(result);
    Response.Flush();
    Response.End();

But i am getting this error as -

System.Web.HttpException (0x80004005): Server cannot set content type after HTTP headers have been sent.

I tried changing Response.Buffer to BufferOutput but still getting the same error. What am I missing here?

1 Answer 1

0

In the code below, i tried to download an excel file as -

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader($"content-disposition", "attachment;filename=" + "{reportFileName}" + ".xlsx");
        using (System.IO.MemoryStream MyMemoryStream = new MemoryStream())
        {
            MyMemoryStream.Write(result, 0 , result.Length);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();

            return File(MyMemoryStream.ToArray(), "application/octet-stream");
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.