javascript-使用Guzzle和Ajax从Dropbox下载文件

我正在尝试使用Guzzle3和Ajax从Dropbox下载文件.当我使用Guzzle发送发帖请求时,我在控制台上收到以下原始代码:

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/mazedulislamkhan@gmail.com/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��

这是我正在使用的Guzzle3代码:

$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();
        }

这是我正在使用的Ajax代码:

$.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);
    }
});

任何人都可以帮助我如何从Ajax请求中下载文件.我也尝试过使用base64_encode结果,但是它仍然不起作用,因为我不仅会下载图像文件.

解决方法:

我建议改用dropbox API.

如文档的下载部分所示,在身份验证之后,您可以使用以下方式读取(下载)文件:

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

附言
如注释下面的建议,使用API​​或任何其他方法,您必须适当设置标头(根据您要下载的文件类型)

上一篇:php-如何使用guzzleHttp创建请求?


下一篇:对于NCover 测试的结果显示格式的修改