[CLOSED] How to download a file in DirectEvent?

  1. #1

    [CLOSED] How to download a file in DirectEvent?

    Hi,

    I have a question that probably has to do with the recently closed thread at http://forums.ext.net/showthread.php...ng-BADRESPONSE.

    Here's the fragments of my code:

                                <ext:Button ID="ButtonView" runat="server" Text="View">
                                    <DirectEvents>
                                        <Click OnEvent="ButtonView_Click" IsUpload="true">
                                            <EventMask ShowMask="true" />
                                            <ExtraParams>
                                                <ext:Parameter Name="Id" Value="#{GridPanel1}.getSelectionModel().getSelected().get('Id')"
                                                    Mode="Raw">
                                                </ext:Parameter>
                                            </ExtraParams>
                                        </Click>
                                    </DirectEvents>
                                </ext:Button>
            protected void ButtonView_Click(object sender, DirectEventArgs e)
            {
                int id = Convert.ToInt32(e.ExtraParams["Id"]);
    
                //Obtain the byte array from the database based on the document Id
                byte[] docData=GetDocumentData();
                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();
            }
    I'm able to download and open the document OK. However, the event mask never disappears and I'm not sure overall whether clearing, flushing and ending the Response is the correct approach for the purpose. Please advise the best practice.

    Thanks,

    Vadym
    Last edited by Daniil; Feb 23, 2012 at 3:53 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Well, the best practice is:

    1. Set up a correct ContentType.
    2. Set up required Headers.
    3. Write a file.
    4. Call the End method.

    Probably, some types of files might require something else.

    the event mask never disappears
    This is the limitation of AJAX downloading - no success/failure events. So, it's impossible to hide after downloading. There is no way to avoid it.

    If masking is required, when you should look into, for example, downloading via Flash.
  3. #3
    Thank you Daniil!

    Is there any way to arrange the file download method as asynchronous callback invoked from the DirectEvent? Then, the event code control would return clearing the mask? The reason I'm asking being that I may need to do some non-instantanious processing in the event and having a mask on the screen while doing that would be great.

    Thanks,

    Vadym
  4. #4
    No, unfortunately, I don't think it's possible.
  5. #5
    Thanks Daniil,

    I'll try to make do without a mask. In that regard, I might have some more questions to to be able to meet my requirements. For now, that has answered my question.

    Thanks,

    Vadym
  6. #6
    Here is some approach regarding to masking for a download AJAX request. It can be helpful on some scenarios.
    http://forums.ext.net/showthread.php...ll=1#post83757
  7. #7
    Quote Originally Posted by Daniil View Post
    Here is some approach regarding to masking for a download AJAX request. It can be helpful on some scenarios.
    http://forums.ext.net/showthread.php...ll=1#post83757
    Thanks for the update Daniil!

    Vadym

Similar Threads

  1. [CLOSED] DirectEvent File Download plus MaskEdit issue
    By digitek in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Jun 11, 2012, 2:08 PM
  2. [CLOSED] [1.0] DirectEvent File Download
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 6
    Last Post: Jan 21, 2011, 6:04 PM
  3. [CLOSED] [1.0] - MVC - DirectEvent Download File
    By drkoh in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jul 23, 2010, 8:26 PM
  4. [CLOSED] Download a file in an MVC app
    By Stefanaccio in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jul 02, 2010, 9:01 PM
  5. [CLOSED] [1.0] File download in MVC
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 23, 2009, 4:43 PM

Posting Permissions