Laravel Dropbox API v2-上传时为空文件 [英] Laravel Dropbox API v2 - Empty File on Upload

查看:64
本文介绍了Laravel Dropbox API v2-上传时为空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到目前为止所拥有的

现在,我在laravel用户和Dropbox API之间具有有效的oauth2身份验证.每个用户都可以将文件上传到他们的个人文件夹中.

Right now I have a working oauth2 authentication between a laravel user and the dropbox API. Every user can upload files to their personal folder.

问题

在使用Dropbox API v2使用laravel上传文件之后,我可以看到上传了一个空文件(0字节).

After Uploading a file with laravel with the Dropbox API v2 I can see that there is a empty (0 Bytes) file uploaded.

用于完成此任务:

  • Laravel
  • 枪口
  • Dropbox API库

我想念什么?

代码

我处理表单的功能如下:

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');

Dropbox库中的我的文件->上载功能如下:

And my files->upload function in my Dropbox Library looks like this:

$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
   ]);

正如我所说,该文件已成功上传.名称正确,但0字节.如此空的文件.

The file, as I said, gets uploaded successfully. Correct name, but 0 Bytes. So empty file.

非常感谢您的帮助!

更新

使用以下代码,我成功了.我的问题是,是否有更好的"Laravel-Like"解决方案而不是使用 fopen ?

With the following code I made it work. My question is though if there is a better "Laravel-Like" Solution instead of using 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"),
        ]);

推荐答案

@Greg的提及方式(

How @Greg mentioned (see cross-linking reference) I was able to solve this issue by using

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

代替

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

这就是Greg所说的,因为在Curl请求中使用了 data-binary .其他HTTP客户端(例如Guzzle)使用不同的名称.

This is, how Greg mentioned, because data-binary is used in Curl requests. Other HTTP Clients, like Guzzle in my case use different names.

这篇关于Laravel Dropbox API v2-上传时为空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆