简体   繁体   English

Laravel Dropbox API v2 - 上传时为空文件

[英]Laravel Dropbox API v2 - Empty File on Upload

What I have till now到目前为止我所拥有的

Right now I have a working oauth2 authentication between a laravel user and the dropbox API.现在我在 laravel 用户和 dropbox API 之间有一个有效的 oauth2 身份验证。 Every user can upload files to their personal folder.每个用户都可以将文件上传到他们的个人文件夹。

The Problem问题

After Uploading a file with laravel with the Dropbox API v2 I can see that there is a empty (0 Bytes) file uploaded.使用 Dropbox API v2 上传带有 laravel 的文件后,我可以看到上传了一个空(0 字节)文件。

Used to accomplish this task:用于完成此任务:

  • Laravel Laravel
  • Guzzle狂饮
  • Dropbox API Library Dropbox API 库

What am I missing?我错过了什么?

The Code编码

My function for processing a form looks like this:我处理表单的函数如下所示:

$formFile = $request->file('fileToUpload');
$path = $formFile->getClientOriginalName();
$file = $formFile->getPathName();
$result = Dropbox::files()->upload($path, $file);
return redirect('dropboxfiles');

And my files->upload function in my Dropbox Library looks like this:我的 Dropbox 库中的 files->upload 功能如下所示:

$client = new Client;

   $response = $client->post("https://content.dropboxapi.com/2/files/upload", [
      'headers' => [
         'Authorization' => 'Bearer '.$this->getAccessToken(),
         'Content-Type' => 'application/octet-stream',
         'Dropbox-API-Arg' => json_encode([
            'path' => $path,
            'mode' => 'add',
            'autorename' => true,
            'mute' => true,
            'strict_conflict' => false
          ])
       ],
       'data-binary' => '@'.$file
   ]);

The file, as I said, gets uploaded successfully.正如我所说,该文件已成功上传。 Correct name, but 0 Bytes.正确的名称,但 0 字节。 So empty file.所以空文件。

Thank you so much in advance for your help!非常感谢您的帮助!

Update更新

With the following code I made it work.使用以下代码我使它工作。 My question is though if there is a better "Laravel-Like" Solution instead of using fopen ?我的问题是,是否有更好的“类似 Laravel”的解决方案而不是使用fopen

$response = $client->post("https://content.dropboxapi.com/2/files/upload", [
            'headers' => [
                'Authorization' => 'Bearer '.$this->getAccessToken(),
                'Dropbox-API-Arg' => json_encode([
                    'path' => $path,
                    'mode' => 'add',
                    'autorename' => true,
                    'mute' => true,
                    'strict_conflict' => false
                ]),
                'Content-Type' => 'application/octet-stream',
            ],
            'body' => fopen($file, "r"),
        ]);

How @Greg mentioned ( see cross-linking reference) I was able to solve this issue by using @Greg 如何提到( 参见交叉链接参考)我能够通过使用解决这个问题

'body' => fopen($file, "r"),

instead of代替

'data-binary' => '@'.$file

This is, how Greg mentioned, because data-binary is used in Curl requests.这是 Greg 提到的,因为data-binary用于 Curl 请求。 Other HTTP Clients, like Guzzle in my case use different names.其他 HTTP 客户端,比如我的 Guzzle 使用不同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM