-1

先感谢您,

我有一个控制器功能,例如

public function storeBlog(Request $request)
{
  // Here i am receiving file like $request->file('image');
}

现在我想将该文件发送到 API 端点,例如

Http::post('http://example.com/v1/blog/store', $request->all());

我收到了所有请求,但没有收到文件,我知道我们需要将POST数据作为 a传递,multipart但我不知道如何

谁能帮忙

4

1 回答 1

9

您应该使用 Http::attach 上传文件。

public function storeBlog(Request $request)
{
    // check file is present and has no problem uploading it
    if ($request->hasFile('image') && $request->file('photo')->isValid()) {
        // get Illuminate\Http\UploadedFile instance
        $image = $request->file('image');

        // post request with attachment
        Http::attach('attachment', file_get_contents($image), 'image.jpg')
            ->post('example.com/v1/blog/store', $request->all());
    } else {
        Http::post('http://example.com/v1/blog/store', $request->all());
    }
}
于 2020-09-15T08:42:09.003 回答