Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • stephen 75 posts 116 karma points
    Mar 01, 2017 @ 12:06
    stephen
    0

    OutputStream is not available when a custom TextWriter is used

    I am trying to utilise

    HTTPPostBinding.SendResponse(HttpResponseBase httpresponse, string targeturl, XmlElement myxmlelement, string relaystate)

    but I get the error message:

    OutputStream is not available when a custom TextWriter is used

    The controller this sits in is a SurfaceController rather than a Controller.

    I am starting to think this is maybe a surfacecontroller issue.

  • Simon 692 posts 1068 karma points
    May 02, 2018 @ 07:35
    Simon
    0

    Have you solved this issue?

  • Mihir Ajmera 32 posts 143 karma points
    Apr 07, 2020 @ 04:35
    Mihir Ajmera
    0

    Hi Stephen,

    There was the same problem facing me in which the surface controller can`t export to CSV file and get the same above error.

    So the solution is calling the surface controller and try to export excel data.

    Please the same thing to do in the view template.

    Refer below code which I place in my ExportToCSV.cshtml template view.

    List<AttorneyExportList> attorneyExportLists = new List<AttorneyExportList>();
    var contentData = Services.ContentService.GetPagedChildren(Convert.ToInt32(ConfigurationManager.AppSettings["AttorneyListID"]), 0, Int32.MaxValue, out _);
    foreach (var attorneyContent in contentData)
    {
        var attorneyExport = new AttorneyExportList();
        if (attorneyContent.HasProperty("firstName"))
        {
            if (attorneyContent.Properties["firstName"].GetValue() != null)
            {
                attorneyExport.firstName = attorneyContent.Properties["firstName"].GetValue().ToString();
            }
    
        }
        if (attorneyContent.HasProperty("middleName"))
        {
            if (attorneyContent.Properties["middleName"].GetValue() != null)
            {
                attorneyExport.middleName = attorneyContent.Properties["middleName"].GetValue().ToString();
            }
    
        }
        if (attorneyContent.HasProperty("lastName"))
        {
            if (attorneyContent.Properties["lastName"].GetValue() != null)
            {
                attorneyExport.lastName = attorneyContent.Properties["lastName"].GetValue().ToString();
            }
    
        }
        attorneyExportLists.Add(attorneyExport);
    }
    
    if (attorneyExportLists.Count > 0)
    {
        var attorneyDataTable = CustomHelper.ToDataTable(attorneyExportLists);
        XLWorkbook wb = new XLWorkbook();
    
        var ws = wb.Worksheets.Add(attorneyDataTable, "AttorneyExportsList");
        ws.Row(1).Style.Font.Bold = true;
        var firstCell = ws.FirstCellUsed();
        var lastCell = ws.LastCellUsed();
        var range = ws.Range(firstCell.Address, lastCell.Address);
        range.Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
        var fullBorderRange = string.Format("{0}:{1}", firstCell.Address.ToString(), lastCell.Address.ToString());
        ws.Range(fullBorderRange).Style.Border.TopBorder = XLBorderStyleValues.Thin;
        ws.Range(fullBorderRange).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
        ws.Range(fullBorderRange).Style.Border.RightBorder = XLBorderStyleValues.Thin;
        ws.Range(fullBorderRange).Style.Border.BottomBorder = XLBorderStyleValues.Thin;
        ws.Tables.FirstOrDefault().ShowAutoFilter = false;
        ws.Tables.FirstOrDefault().Theme = XLTableTheme.None;
    
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=AttorneyExportsList.csv");
        MemoryStream MyMemoryStream = new MemoryStream();
        wb.SaveAs(MyMemoryStream);
        MyMemoryStream.WriteTo(Response.OutputStream);
        Response.Flush();
        Response.End();
    }
    

    In the above AttorneyExportList is a custom model class.

    public class AttorneyExportList
    {
        public string firstName { get; set; }
        public string middleName { get; set; }
        public string lastName { get; set; }
        public string nameForSearch { get; set; }
        public string orderName { get; set; }
        public string urlTwitter { get; set; }
        public string videoTitle { get; set; }
        public string youTubeVideoUrl { get; set; }
    }
    

    Let me know if not working above the solution.

Please Sign in or register to post replies

Write your reply to:

Draft