开发者

ExtJS grid form with document in MySQL

开发者 https://www.devze.com 2023-03-01 16:11 出处:网络
I have a grid whith the data that come from a database. Each row of the grid containt these fields : document name, owner, version etc. The content of the document is in a blob.

I have a grid whith the data that come from a database. Each row of the grid containt these fields : document name, owner, version etc. The content of the document is in a blob.

I want, when the user double-click on a row, to download the content of the document. The best way will be a pop-up that ask a confirmation. The grid must stay on the screen.

I'm able to read the content of the document with an ajax request and put the result in a variable.

                            Ext.Ajax.request({
                            url: '../controleurs/c_get_doc_content.php',
                            method: 'POST',
                            params: { dep: r.data.nom_document },
                            success: function(response, opts) {console.log(response);console.log(opts)},
                            requestcomplete: function (conn, response, options) {},
                            failure: function() {console.log('failure');},
                            head开发者_如何转开发ers: { 'my-header': 'foo' },

How can I save the content of the document to a file on the user PC ?


The best solution for this is server side. Instead of passing the blob to the JS, you just pass an ID. Then when the user chooses to download the file, you call the server and it returns a document that can be downloaded.

You would just stream that content as an attachment instead of sending it with the AJAX request

in PHP you use the following code to make sure a file is downloaded and the save as dialog is displayed.

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}


Set the content type of your PHP response to something the browser would want to save....

0

精彩评论

暂无评论...
验证码 换一张
取 消