【问题标题】:how to upload image using http client laravel to API如何使用 http 客户端 laravel 将图像上传到 API
【发布时间】:2021-10-27 22:46:15
【问题描述】:

我想使用 http 客户端 laravel 更新/上传图像。我确实更新了数据,但图像没有更新。 API 是 blob 类型。我尝试在邮递员中使用表单数据文件,它确实有效。但我不知道为什么它在我的 laravel 应用程序中不起作用。

这是我的代码

$image = $request->file('profile_image');
        if ($image != null) {
            $contents = $image->openFile()->fread($image->getSize());
        }
        $response = Http::withToken($request->session()->get('user'))->acceptJson()->attach('image', file_get_contents($image))->post("https://voice.infidea.id/api/gateway/update-user", [
            'id' => $user['id'],
            'name' => $request->name,
            'email' => $request->email,
            'contact' => $request->contact
        ]);

我也尝试使用 $content 但仍然没有用。只有更新的字符串数据。

这就是我在邮递员中上传的方式

请有人知道为什么会这样,请帮助我真的很感激。

【问题讨论】:

  • 在我的邮递员那里没问题。在代码中挖掘何时从代码中发布 API

标签: php laravel httpclient laravel-8 guzzle


【解决方案1】:

您需要为 file_get_contents() 指定完整的文件路径, 改成这个

file_get_contents($request->file('profile_image')->getRealPath()));
// or
file_get_contents($request->profile_image->path());

你只需放置图片的临时路径或完整路径

$response = Http::withToken($request->session()->get('user'))->acceptJson()->attach('image', file_get_contents($request->file('profile_image')->getRealPath()))->post("https://voice.infidea.id/api/gateway/update-user", [
            'id' => $user['id'],
            'name' => $request->name,
            'email' => $request->email,
            'contact' => $request->contact
        ]);

【讨论】:

    【解决方案2】:

    我认为$image = $request->file('profile_image'); 的值未在您使用image 的邮递员中设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-15
      • 2015-03-07
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多