[CLOSED] Open file from grid

  1. #1

    [CLOSED] Open file from grid

    Hi all,

    I have to download/open file from grid. I click in ID cell of the record grid to get the ID of my business object and then server side open file. The file is directly saved in DB. I saw example exporting grid via AjaxEvent, so I did the same.
    The problem is that sometimes I successfully download file, sometimes get Error AjaxEvent!

    Here is my code:

    
    <AjaxEvents>
                                    <CellClick OnEvent="OpenDoc" IsUpload="true" Failure="Ext.MessageBox.alert('Load failed', 'Error during ajax event!');">
                                        <ExtraParams>
                                            <ext:Parameter Name="idDoc" Value="params[0].getStore().getAt(params[1]).id" Mode="Raw" />
                                        </ExtraParams>
                                    </CellClick>
                                </AjaxEvents>
    Code behind

    
    // inside AjaxEvent block
    
    ...
    
    Response.Clear();
    
    object _objDoc = _documento.FileDocumento;
                    if (_objDoc == null) return;
    
                    byte[] _document = (byte[])_objDoc;
    
                    object _objContentType = _documento.ContentTypeDoc;
                    if (_objContentType == null) return;
    
                    object _objNomeDoc = _documento.NomeDocumento;
                    if (_objNomeDoc == null) return;
    
                    using (MemoryStream stream = new MemoryStream())
                    {
                        
                        stream.Write(_document, 0, _&#100;ocument.Length);
                        byte[] buffer = stream.GetBuffer();
                        Response.Buffer = false;
                        Response.AppendHeader("Content-Type", _objContentType.ToString());
                        Response.AppendHeader("Content-Transfer-Encoding", "binary");
                        Response.AppendHeader("Content-Disposition", "attachment; filename=" + _objNomeDoc.ToString());
                        Response.BinaryWrite(buffer);
                        
                    }
    
    Response.End();
    First: is my code wrong? Do I miss anything?
    Second: I tried different CellClick properties, where can I find docs about Type, IsUpload, Buffer, CleanRequest ect. properties?

    Thanx

    Matteo
  2. #2

    RE: [CLOSED] Open file from grid

    Hi Matteo,

    What error did you get?


    Type - if Type=Submit the form will be submitted otherwise only params


    IsUpload -*True if the form object is a file upload (will usually be automatically detected).
    File uploads are not performed using normal "Ajax" techniques, that is they are not performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the DOM <form> element temporarily modified to have its target set to refer to a dynamically generated, hidden <iframe> which is inserted into the document but removed after the return data has been gathered.
    The server response is parsed by the browser to create the document for the IFRAME. If the server is using JSON to send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.
    The response text is retrieved from the document, and a fake XMLHttpRequest object is created containing a responseText property in order to conform to the requirements of event handlers and callbacks.

    This option is required also if you return file in response stream



    Buffer {Number} : Causes the handler to be scheduled to run in an Ext.util.DelayedTask delayed by the specified number of milliseconds. If the event fires again within that time, the original handler is not invoked, but the new handler is scheduled in its place.



    CleanRequest - If true then request will sent only sent params in query string. Useful if ajax request using with web service
    https://examples1.ext.net/#/Events/AjaxEvents/WebService/







  3. #3

    RE: [CLOSED] Open file from grid

    Hi Vlad,

    thanx for clarifications.

    About my issue, it's a bit confusing, I get code working and not.

    First, if I don't specify IsUpload in CellClick, I get the error you can see in attachment.
    Then, I have 3 cases:

    1 - at cellclick I immediately get Undefined error, without entering server side code.

    2 - Code enters in my AjaxEvent block and I get a NullReferenceException about the ExtraParams:

    
    // e.ExtraParams["idDoc"] is NULL
    int id = int.Parse(e.ExtraParams["idDoc"].ToString());
    3 - Code executes and I manage to download file.

    I have no idea of how to solve this issue.

    How would you download a file, from grid, saved in the DB table?

    Thanx

    Matteo
  4. #4

    RE: [CLOSED] Open file from grid

    Hi Matteo,

    I can't say any concreate without a sample code which I can test.
    About downloading file from DB. I preffer to download file with distinct http handler*


    For example for cell click you can define renderer which renders anchor link to this handler


    <a hfer="myhandler.ashx?id=fileID">Download file</a>


    The handler will be retrieve file from DB and sent back to the client




  5. #5

    RE: [CLOSED] Open file from grid

    Hi Vlad,

    all right, I solved with HttpHandler.

    Thank you

    Matteo

Similar Threads

  1. File Upload Field Browse not Open ?
    By nazmulrockon in forum 2.x Help
    Replies: 9
    Last Post: Jun 21, 2012, 9:01 AM
  2. Replies: 1
    Last Post: Jun 16, 2010, 9:04 PM
  3. Download and open a file in a separate window
    By unaltro2 in forum 1.x Help
    Replies: 2
    Last Post: Jan 21, 2010, 11:55 AM
  4. [CLOSED] Error while trying to open a file (.jpg,pdf,doc, etc.)
    By Etisbew in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Aug 25, 2009, 8:23 AM
  5. Command column to open a file from db.
    By grmontero in forum 1.x Help
    Replies: 1
    Last Post: Mar 15, 2009, 7:30 AM

Posting Permissions