[CLOSED] BADRESPONSE downloading a file

  1. #1

    [CLOSED] BADRESPONSE downloading a file

    Hi,

    I have a direct method that writes bytes to Response OutputStream called on a hyperlink click JS event. The hyperlink is created from a text value of the GridPanel cell in a Renderer function. I'm getting a popup with
    BADRESPONSE: Invalid character
    .

    I believe the workaround for the button doing the same was to set
    IsUpload="true"
    in the markup. What do I have to do to make the hyperlink scenario work?

    Thanks,

    Vadym
    Last edited by Daniil; Feb 24, 2012 at 4:24 PM. Reason: [CLOSED]
  2. #2
    Quote Originally Posted by vadym.f View Post
    Hi,

    I have a direct method that writes bytes to Response OutputStream called on a hyperlink click JS event. The hyperlink is created from a text value of the GridPanel cell in a Renderer function. I'm getting a popup with
    BADRESPONSE: Invalid character
    .

    I believe the workaround for the button doing the same was to set
    IsUpload="true"
    in the markup. What do I have to do to make the hyperlink scenario work?

    Thanks,

    Vadym
    Please post a full (but simplified) .aspx code sample demonstrating the scenario and how you have all these parts configured. Please reduce the minimum required to reproduce the issue (typically <100 lines).
    Geoffrey McGill
    Founder
  3. #3
    Hi,

    Please look at the Ext.Net.DirectResponse sources, how IsUpload is used.

    Return method
    public virtual void Return()
    {
        if (!this.IsUpload && !this.NativeUpload)
        {
            CompressionUtils.GZipAndSend(this);
        }
        else
        {
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.Write(this.ToString());
        }
    }
    ToString method
    public override string ToString()
    {
        if (this.Script.IsNotEmpty() && !this.Script.StartsWith("<string>"))
        {
            this.Script = "<string>" + this.Script;
        }
    
        string serialize = new ClientConfig().Serialize(this);
    
        if (!this.internalUsing && (this.IsUpload || this.NativeUpload))
        {
            serialize = "<textarea>{0}</textarea>".FormatWith(serialize);
        }
    
        if (!this.internalUsing && HttpContext.Current != null && HttpContext.Current.CurrentHandler is Page)
        {
            HttpContext.Current.Items["Ext.Net.Direct.Response.Manual"] = true;
    
            return "<Ext.Net.Direct.Response.Manual>".ConcatWith(serialize, "</Ext.Net.Direct.Response.Manual>");
        }
    
        return serialize;
    }
    I think you should implement these parts in your code.
    HttpContext.Current.Response.ContentType = "text/html";
    HttpContext.Current.Response.Write(this.ToString());
    and
    serialize = "<textarea>{0}</textarea>".FormatWith(serialize);
  4. #4
    Thanks for your responses! The initial code sample is in http://forums.ext.net/showthread.php...-on-the-client.

    I figured out how to achieve the objective as follows (please advise if there's a better way):

            
    var linkRenderer = function (value, metadata, record) {
        value = String.format('<a href="javascript:void();" onClick="Ext.net.DirectMethods.ViewDocument(getRecord(), {isUpload: true});" target="_top">{0}</a>', value);
        return value;
     };
    
    var getRecord = function () {
         var record = GridPanel1.getSelectionModel().getSelected();
         return record.get("Id");
    }
            <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
                <Store>
                    <ext:Store ID="Store1" runat="server">
                        <Reader>
                            <ext:JsonReader>
                                <Fields>
                                    <ext:RecordField Name="Id" />
                                    <ext:RecordField Name="DocName" />
                                </Fields>
                            </ext:JsonReader>
                        </Reader>
                    </ext:Store>
                </Store>
                <ColumnModel runat="server">
                    <Columns>
                        <ext:Column ColumnID="Id" Header="Id" DataIndex="Id" Hidden="true" />
                        <ext:Column ColumnID="DocName" Header="Document" DataIndex="DocName">
                            <Renderer Fn="linkRenderer" />
                        </ext:Column>
                    </Columns>
                </ColumnModel>
            </ext:GridPanel>
            [DirectMethod]
            public void ViewDocument(int documentId)
            {
                byte[] docData = GetDocumentData();   // Getting a byte array
                string docName="Test.docx";
    
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.Buffer = true;
    
                HttpContext.Current.Response.ContentType = "application/msword";
                HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
                HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", docName));
                HttpContext.Current.Response.AddHeader("Content-Length", docData.Length.ToString());
    
                HttpContext.Current.Response.OutputStream.Write(docData, 0, docData.Length);
    
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
    Thanks,

    Vadym
  5. #5
    I misunderstood a bit your initial post.

    Yes, you just need to set up
    isUpload : true
    for the DirectMethod config.

    Also, please note, that file downloading requires any <form> HTML element on the page.
  6. #6
    Thanks Daniil,

    It's working for me as expected.

    Vadym

Similar Threads

  1. Replies: 2
    Last Post: Aug 30, 2013, 1:48 AM
  2. Replies: 1
    Last Post: Nov 02, 2012, 4:07 AM
  3. [CLOSED] Downloading a file from a menu directEvent
    By feanor91 in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jun 28, 2012, 7:19 AM
  4. downloading PDF file
    By unaltro2 in forum 1.x Help
    Replies: 0
    Last Post: Mar 10, 2011, 5:37 PM
  5. [CLOSED] DirectEvent from webService downloading file
    By pil0t in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 04, 2010, 6:05 AM

Posting Permissions