[我到现在为止有什么
现在,我在laravel用户和Dropbox API之间具有有效的oauth2身份验证。每个用户都可以将文件上传到其个人文件夹。
问题
[在使用Dropbox API v2使用laravel上传文件后,我看到上传了一个空(0字节)文件。
用于完成此任务:
我想念什么?
代码
我处理表单的功能如下:
$formFile = $request->file('fileToUpload');
$path = $formFile->getClientOriginalName();
$file = $formFile->getPathName();
$result = Dropbox::files()->upload($path, $file);
return redirect('dropboxfiles');
以及我的文件-> Dropbox库中的上载功能如下:
$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字节。如此空文件。
非常感谢您的帮助!
更新
使用以下代码,我成功了。我的问题是,是否有更好的“ Laravel-Like”解决方案而不是使用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如何提及(see交联参考),我能够使用]解决此问题>
'body' => fopen($file, "r"),
而不是
'data-binary' => '@'.$file
这就是Greg所说的,因为在Curl请求中使用了
data-binary
。其他HTTP客户端(例如Guzzle)使用不同的名称。