Search code examples
asp.netparameter-passinggeneric-handler

How to send a byte array parameter to a generic handler from the codebehind file in vb asp.net


Any server based (not occurring on localHost) IIS loses the session because new windows are started under new processes - "per google search results"

In my case I'm using Session("fileData") = fileData

this is how i redirect my page

Page.ClientScript.RegisterStartupScript(Me.GetType(), "OpenPDFScript", "window.open('OpenPDFNewWindowHandler.ashx', '_blank');", True)

Instead of saving my fileData to a session variable, can i pass it as a parameter to the generic handler? Or maybe their is a better way?


Solution

  • Okay so, the other option is probably better due to not sending a full file, but...

    On your front end:

    <% var a = new byte[] { 1, 1, 2, 3, 5, 8 };  %>
    <script>
        var data = "<% Response.Write(Convert.ToBase64String(a)); %>";
        function openhandler() {
            var f = document.createElement("form");
            f.method = "post";
            f.action = "Handler1.ashx";
            f.target = "Handler1Window";
            var i = document.createElement("input");
            i.name = "data";
            i.value = data;
            f.appendChild(i);
            f.style.position = "absolute";
            f.style.display = "block";
            f.style.width = "0px";
            f.style.height = "0px";
            f.style.overflow = "hidden";
            document.body.appendChild(f);
            f.submit();
        }
    </script>
    

    And in your handler:

    public void ProcessRequest(HttpContext context)
        {
            var bytes = Convert.FromBase64String(context.Request.Form["data"]);
            context.Response.ContentType = "text/plain";
            context.Response.Write(String.Join(",", bytes));
        }