Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files from Dropbox using Guzzle and Ajax

I'm trying to download files from Dropbox using Guzzle3 and Ajax. When I'm sending a post request using Guzzle I'm getting the following raw code on my console:

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 28 Dec 2015 09:34:20 GMT
Content-Type: application/octet-stream
Content-Length: 546000
Connection: keep-alive
pragma: no-cache
cache-control: no-cache
original-content-length: 546000
dropbox-api-result: {"name": "picture.jpg", "path_lower": "/box/[email protected]/picture.jpg", "id": "id:QzsqO9c4vG0AAAAAAAABzw", "client_modified": "2015-12-27T20:58:19Z", "server_modified": "2015-12-27T20:58:19Z", "rev": "4db24d66f17", "size": 546000, "media_info": {".tag": "metadata", "metadata": {".tag": "photo", "dimensions": {"height": 1281, "width": 1032}}}}
X-Server-Response-Time: 231
X-Dropbox-Request-Id: 3d610dd4a84f6717edd06dc5a2983a81
X-Robots-Tag: noindex, nofollow, noimageindex

����ExifII*��Duckyd��,http://ns.adobe.com/xap/1.0/<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.5-c014 79.151481, 2013/03/13-12:09:15        "> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stRef="http://ns.adobe.com/xap/1.0/sType/ResourceRef#" xmp:CreatorTool="Adobe Photoshop CC (Macintosh)" xmpMM:InstanceID="xmp.iid:D0AC9B83656611E49FF7AB801A4B82BC" xmpMM:DocumentID="xmp.did:D0AC9B84656611E49FF7AB801A4B82BC"> <xmpMM:DerivedFrom stRef:instanceID="xmp.iid:D0AC9B81656611E49FF7AB801A4B82BC" stRef:documentID="xmp.did:D0AC9B82656611E49FF7AB801A4B82BC"/> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="r"?>��Adobed���������   
    !1AQa"  q��2������BR#3
�br$��CS���s4%�c��T���Dt�d�5u���6'(!1AQaq"2�������BR#3��br$�4C���S%�Ds56��?�̋�_��<�=�,R���L���k�������b�5�E��?���"?��G��������?�'���#��E�Q��A�!�>h�Q��I/o������j)ϭG1/l�H��1�K��5��8p�TStjM��;ܯ��c��H�p�t}ʂ[�"Hgd���!p?ҏ��o��(�YJ����a��>�Y4].����������Y�?��Me��ߚ^���������ffl�19f�(>���~>(�P���>�~���,3���8�YC���r�X���F����,������3d�E���?�߸��������Q}i���?�[>A!�Ӛ�S��v'�����(�b�S�̸|���<�IJ� x@T��H�0}m��$��\��  ��wa,�Ȓ�(Ve9w��Ș��?�S�9�}I���!�p��t�K8�}a���8|"���T��Q8%Fg��2����o��JH���rH��g4��G�8O�����ـ�`�5��s�3�U�Bws�y$�$��s�3�Q?�0}d�$��W���g��H_��?��&�����g��H>�\Ѩ��J���G��K����E�s�3�T���̣QC�s�7����2�QC���=�?�W.e�?5����>�\�j)?P���{%� ��(�Q��<V�;{!>�_ԁ2�~w�N0��<�]c�����4���<�ME ��W�a>���Ѩ�y���_ި>��?4j(TL����?8>��?4j(����/կ���3?4��j�ux�<J��    �<  o4��_���X�ά;����1��QDW����������$�y�QI�c����J�])s?4��p���9a�0��P����P�������b�[ĥԋ:��?���[ģQ�C9���cWģR<��q�%��0�Px�j(���?�_�
c�  ����5�ht&����N���x�O��`�9��N?c�)#ǿ��N�Q�V'�������>�\�F�,��q���a
ZC��H�����#��!��U�#�����r�"����+Q���wB�%?M|��"p�V����5H9#M~�8e���!����JZp��I9�H���a9�����3)� �#{ߵk�v�9����n��75+{f.hkZ&����l�}�k�   q����}\���9Yo#����چ
�����È�ĕa���$ x%��Tp$��(OO�Q���9��'��?���G�1_w���R���   +�3�?�``�!  +�3�?HV`���3�?�a#H@������BCȦ�����?]�St���?p�@�@��J�����?(M!;@�Bk��A�x�D|P��%i=��i�gQ���O,�J�s����o ���Ss�w��`�,rJ)�)2r|���8��'B�rp�|?XIU�����X�C��  �ZB��e+��   ����Y��h@�@���
�>H餅�@�����e�`�[�W14��� Фdy�<[8��|`�.����y�a4��gV2l}��`҃��s���0�<SY8y�/��a��������A���v7���sC#��_��<PG$<��������Dg�0���&�2YY����B��,�!I*�h��0���S�����T�&C$��NG��0�+"G��

Here's the Guzzle3 code that I'm using:

$client = new Client();
        $request = $client->post('https://content.dropboxapi.com/2/files/download');
        $request->setHeaders([
            'Authorization' => 'Bearer ' . self::getAccessToken(),
            'Dropbox-API-Arg' => '{"path": "/Box/' . $dropboxPath . '/' . $fileName . '"}'
        ]);

        try {
            $response = $request->send();

            return $response;
        } catch (BadResponseException $e) {

            return $e->getResponse();
        }

Here's the Ajax code that I'm using:

$.ajax({
    type: 'POST',
    url: '/downloaddropbox/' + filePath + '/' + fileName,
    data: 'filePath=' + filePath + '&fileName=' + fileName,
    beforeSend: function () {
        Materialize.toast('Please, wait while we\'re processing your download request.', 4000);
        $('.progress').show();
            },
    complete: function () {
        $('.progress').hide();
    },
    success: function (data) {
        console.log(data);
            },
    error: function () {
        Materialize.toast('Something goes wrong. Please, try again later!', 4000);
    }
});

Can anyone please help how may I download the file from the Ajax request. I've also tried by using base64_encode the result but it still doesn't work as I'll not only downloading image files.

like image 653
Md Mazedul Islam Khan Avatar asked Nov 27 '25 13:11

Md Mazedul Islam Khan


1 Answers

I would suggest to use the dropbox API instead.

As demonstrated in the documentation's download section, after authentication you could read (download) files using something like:

$localFile = fopen("working-draft.txt", "w+b");
$fileMetadata = $dbxClient->getFile("/file-in-dropbox.txt", $localFile);
fclose($localFile);
print_r($fileMetadata);

P.S. as suggested in the comment bellow, using the API or any other methods you have to set the header appropriately (according to the file type you are downloading)

like image 126
Ali Avatar answered Nov 29 '25 02:11

Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!